Variables in JavaScript are like containers that hold reusable data. These data containers need to be declared with some specific keywords in Javascript.
Right now there are 3 ways to declare a keyword in JavaScript:
- var (older way)
- let (introduced in ES6 ✨)
- const (introduced in ES6 ✨)
Before the standardization of ES6 (ES2015), everyone used to declare variables with the var keyword. Now we have
let and const for every possible case.
Rules for using const and let
Follow these two rules to decide:
- Use
constas a constant when you are sure that the variable will not be redeclared. - Use
letfor everything else.
Rules for naming variables
Variable names are case-sensitive, so name and Name both will be considered different variables.
let name = 'Pankaj';let Name = 'Batman';
console.log(name); // 'Pankaj'console.log(Name); // 'Batman'Variable names cannot begin with a number but the numbers can be used in the middle and end of the variable name.
let 1name; // ❌ invalidvar 1name; // ❌ invalidconst 1name; // ❌ invalidlet name1; // ✔ validvar name1; // ✔ validlet my1name; // ✔ validA variable declared with const must be initialized.
// ❌ invalidconst name; // SyntaxError: missing initializer
// ✔ validconst name = 'Pankaj';Variables can start, end, or contain the following:
- Uppercase strings
- Lowercase strings
- Underscores
_ - Dollar sign
$
let _name; // ✔ validvar $name; // ✔ validconst my_name; // ✔ validlet my$name; // ✔ validVariables cannot start, end, or contain symbols and special characters:
let my@name; // ❌ invalidconst #name; // ❌ invalidvar my%name; // ❌ invalidlet -name; // ❌ invalidconst my^name; // ❌ invalidvar *name; // ❌ invalidMultiple variables can be chained by comma, but it’s not considered good practice to do this.
let x, y, z; // ✔ validvar x, y, z; // ✔ validconst x, y, z; // ❌ invalid
// ✔ valid with let, const and varlet x = 1, y = 2, z = 3;const a = 1, b = 2, c = 3;Subsequent declaration of a variable is possible with var but not with let and const.
// ✔ validvar age = 80;var age = 81;console.log(age) // 81// ❌ invalid for let and constlet age = 80;let age = 81;// SyntaxError: Identifier 'age' has already been declared// ✔ valid for var and letlet age = 80;age = 81;console.log(age) // 81// ❌ invalid for constconst name = 'Hulk';name = 'Thor'; // Uncaught TypeError: Assignment to constant variable.// ✔ valid for let, var, and const if the variable is an object/arrayconst hero = {};hero.name = 'Thor'; // ✔
const hero = { name: 'Hulk' };hero.name = 'Thor'; // ✔Notice in the above last example that we are just modifying one of the keys in the object and not replacing the entire object, so it’s working perfectly fine.
Why should we prefer let and const over var
It is good practice to avoid using var declaration in your code. let was introduced to provide a
level of organization while managing the large data structures as it is safer knowing that your variable cannot
be reassigned anywhere in its scope.
Thanks for reading! 🎉