var
var myVar = "Hello"
var myVar = "Hello again" //won't throw an error
- The scope of a var variable is functional scope.
- It can be declared without initialization.
- It can be accessed without initialization as its default value is “undefined”.
var myVar;
console.log(myVar); //undefined
myVar = "Hello there!";
console.log(myVar) // Hello there!
- Hoisting
console.log(myVar);
var myVar = "Hello";
var myVar;
console.log(myVar); //myVar is undefined
myVar = "Hello";
Take a look at this example:
var myVar = 1;
var count = 2;
if(count > 1){
var myVar = "Hello";
}
var result = myVar + 10;
console.log(result); // Hello10
Now here it comes the benefit of using let and const
.let
{
let myVar = 2;
}
console.log(myVar);
// ReferenceError: myVar is not defined
let myVar = 2;
myVar = 10; // can be updated into the scope
let myVar = 1;
//SyntaxError: Identifier 'myVar' has already been declared
let myVar;
This will cause an error
console.log(myVar);
let myVar;
//ReferenceError: Cannot access 'myVar' before initialization
const
- The scope of a const variable is block scope.
{
const myVar = 10;
}
console.log(myVar)
//ReferenceError: myVar is not defined
- It cannot be updated or re-declared into the scope.
const myVar = 1;
myVar = 10;
// TypeError: Assignment to constant variable.
- It cannot be declared without initialization.
const myVar;
console.log(myVar)
//SyntaxError: Missing initializer in const declaration
- Hoistring, const declarations are hoisted to the top but are not initialized. so if you try to access a variable declared with const, then it will throw an error as it is not initialized.
console.log(myVar)
const myVar;
//SyntaxError: Missing initializer in const declaration
When it comes to objects, it's a little bit different.
An object declared with const can not be updated, but... its properties can be updated.
If we declare an object with const and then try to update it like this:
const myObject = {
id: 1,
text: "This is text",
};
myObject = {
id:2,
text: "Updated text",
};
//TypeError: Assignment to constant variable.
But we can update its properties like this:
const myObject = {
id: 1,
text: "I love coding",
};
myObject.id = 2;
myObject['text'] = "I love JavaScript";
console.log(myObject.id) //2
console.log(myObject.text) // I love JavaScript
Summary
- var declarations are globally scoped or function scoped while let and const are block scoped.
- var variables can be updated and re-declared within its scope, let variables can be updated but not re-declared, const variables can neither be updated nor re-declared.
They are all hoisted to the top of their scope. But while var variables are initialized with undefined, let and const variables are not initialized.
- While var and let can be declared without being initialized, const must be initialized during declaration.
- Always use const when you know that the variable value should not be changed
- Use let when you want the variable value to be changed during the code
- Avoid using var unless you know what you are doing.