const. Initially, variables were created using only the
var keyword. But due to some historic drawbacks of using
const were implemented for creating variables. It is recommended that Developers don’t use the
var keyword anymore to declare variables. Irrespective of which keyword you use to get your variable declared, the syntax still remains the same.
As seen on the right, a variable can be created by specifying the keyword, followed by a variable name that will be used to store the value and as well retrieve it later, a simple assignment operator (an equal sign) for assigning variable values, and the value to be stored itself.
<keyword> <variable_name> = <value_to_be_stored>
You can choose to create a local variable or a global variable. Global variables are accessible from anywhere in the program. Local variables are variables declared inside a function, which avoids a conflict with another variable with the same variable name.
Following are the rules that a Developer should follow to properly get variables declared:
- Variable name should only contain alphabets, numbers, $ and _
- Variable name should not start with a number
- Variable names are case-sensitive i.e.
Resultare two different variables
- Variables can’t be named as one of the reserved keywords like
- Variable should use
numOneis preferred over
- Variable can’t have hyphen
-in its name
- Use easy-to-understand names that symbolize the value stored in variables. For e.g. instead of calling a variable
phNum, a Developer can call it
- Don’t use single-letter variable names like
Variables can be created using the
var keyword for variables is discouraged and should be replaced with
const keywords. Variables created using
var keyword are also function-scoped or global-scoped i.e. it is very hard to limit wherein a large block of code the variable should be accessible. Hence, code written using the
var keyword is hard to maintain.
var numOne = 20; var numTwo = 30; var result = numOne + numTwo; console.log(‘Result is: ‘, result);
let keyword was introduced to solve hoisting issues that the
var keyword had.
let variables are block-scoped and are only accessible to where they are declared. This limits the issues of variables being overwritten somewhere else in the code. Apart from this, the variables created using the let keyword follow the same syntax as the ones created using the var keyword. Variables created using let and var keywords can be reassigned a value of a different kind. Hence they are mutable.
let numOne = 20; let numTwo = 30; var result = numOne + numTwo; console.log(‘Result is: ‘, result); // should print 50 numThree = 60; result = numOne + numThree; // reassign result new value console.log(‘Result is: ‘, result); // should print 80
Sometimes, variables created should not change the value assigned to it. This can’t be achieved if you declare a variable using
var keywords. In such cases, a variable should be created using the
const keyword. A variable created using
const∏ can’t change the value assigned to it. It symbolizes constants.
let numOne = 20; let numTwo = 30; const result = numOne + numTwo; console.log(‘Result is: ‘, result); // should print 50 numThree = 60; result = numOne + numThree; // this is not allowed as result is a constant variable console.log(‘Result is: ‘, result); // this will not be executed as above line has error
const variables are also named differently sometimes when they store a value that would be otherwise hard to remember or store. Like private keys, colors, fonts, etc. usually have complex values and hence
const are appropriate for it.
const LIGHT_GRAY = ‘#ccc’; const DARK_GRAY = ‘#eee’;
Get hands-on experience writing code with interactive tutorials in our free online learning platform.
- Free and fun
- Designed for beginners
- No downloads or setup required