Variable naming conventions in JavaScript with example
In this tutorial, you will learn about variable naming conventions with examples in javascript, so while naming your variable keep this rule in your mind.
Following are the variable naming convention.
- The Javascript variable name should start with a letter or combination of letters, a dollar sign ($), or an underscore (_). It must not begin with a number.
let num = 5; // correct let $num = 5; // correct let _num = 5; // correct let 5num = 5; // wrong - The variable name can contain numbers, letters, dollar signs ($), or an underscore. Note that you must not use a space, period (.), or a dash(-) in a variable name.
/* variable contain number, letters or dollar signs ($) */ let num1$ = 10; // correct /* variable should not contain space */ let first name = 'studyfame' // wrong /* variable should not contain dot */ let na.me = 'studyfame' // wrong /* variable should not contain dash(-) */ let na-me = 'studyfame' // wrong - All JavaScript variables are case sensitive,
shopandShopwould be different variable names, but it is bad practice to create two variable names that have the same using different cases. - You cannot use
keywordsorreserved words. Keywords are special words that tell the interpreter to do something. For example, var, let, const, is a keyword used to declare a variable. you cannot use reserved words for variable naming because reserved words are ones that may be used in a future version of JavaScript.
Note
- Always kept the variable name that describes the kind of information the variable hold.
- If your variable name is made of more than one word, then use camel case for writing variable name, for example
firstNamerather thanfirstname