What is a variable?
Now, a user can not directly access a memory location hence variables are used.
A variable is a user defined name that is used to access a memory location. Using a variable, you can store or modify a value at the memory location.
Above illustration declares two variables(digit and lang) pointing to different memory locations having different values.
varkeyword followed by the name of variable which can be any user defined name.
ES6(or ECMAScript6) introduced a different way of declaring variable.
It suggests using
let keyword in place of
Thus, above variable declarations can be modified as below.
If the variable value will never change after initialized for the first time or you do not want it to be changed accidentally, then use
const in place of
const digit = 5; digit = 10; // Illegal
Variable naming rules
Though a variable can be a user defined name but there are few rules that should be followed while declaring a variable.
1. A variable name can not begin with a number.
Thus, 12digit, 0lang are invalid variable declarations. Variable names should begin with a letter or underscore. Examples, _start, digit123, technology etc., are valid variables.
A keyword, also called reserved word, is a word which has some special meaning in the language syntax.
breakis a keyword.