How to Declare Variables in Javascript: Let Vs. Var Vs. Const by@sankalp1122

How to Declare Variables in Javascript: Let Vs. Var Vs. Const

Javascript has many frameworks like Vue, Angular, and libraries like React for frontend Development. Javascript can tackle Backend Development using NodeJS using Node JS. Javascript says nothing is impossible. Javascript has a few ways to declare variables in JS. Let and Const are the ones which you will be using in the Industry. Let is block-scoped and not a global scope local variable used for initializing a statement. Var is defined throughout the program but not if it’s declared in a block.
image
Sankalp Swami HackerNoon profile picture

Sankalp Swami

I m a self learnt Node JS Developer from India.

github social icontwitter social icon

Javascript says nothing is impossible. Javascript has many frameworks like Vue, Angular, and libraries like React for frontend Development, and at the same time, it can even tackle Backend Development using Node JS. For me, watching someone writing Javascript is as soothing as watching this hug:

chandler & joey

chandler & joey

Here are a few ways to declare variables in JS:

LET

Let statement is block-scoped and not a global scope local variable used for initializing a statement. This statement can be used in function as it is function-scoped and one of the main characteristics of let statement is that it can be reassigned.

let name = "Sankalp";
name = "Sankee";
console.log(name); // will print: Sankee

VAR

Var statement is also used for variable declaration and it is global variable. Variable declared with var is defined throughout the program. Var is defined throughout the function but not if it’s declared in a block as it is not block-scoped.

if (true) {
var firstName = 'Sankalp'
let age = 18
}
console.log(firstName) // Will print: Sankalp
console.log(age) // Will print: ReferenceError

Var statement can be reassigned.

var name = 'Sankalp'
name = 'sankee'
console.log(name) // will print: sankee

CONST

When we declare a variable with let, we can reassign its value later on. That’s great but what if we don’t want to reassign its value, for those, we have const. Const is function-scoped and block-scoped and it is not defined throughout the program as it is not global-scoped. If you try to reassign the const variable it will throw a type error.

const name = 'Sankalp';
name = 'Sankee'
console.log(name) // will print: TypeError

const variables in objects and arrays. You cannot overwrite an object but yes you can change its properties.

const person = {
  name: "Sankalp",
  age: 18,
};

person = {
  name: "Sankee", //  this will throw TypeError
};

reassigning a value of keys is possible as mentioned

const person = {
  name: "Sankalp",
  age: 18,
};

person.name = "Sankee";
console.log(person.name); // will print: Sankee

In arrays, you cannot assign an array to a const variable.

const list = [];

list = ["abc"]; // will throw TypeError

But you can push items to the array.

const list = [];

list.push("ABC");
console.log(list); // will print: ['ABC]


Variables declared with var can be accessed even before they are declared. The reason for this behavior is that they are hoisted up to the top of the scope. When you declare some variable without any statement it gets declared as var automatically.

name = "sankee" // defaults to var
console.log(name) // Will print: sankee

Final Verdict

Var variable is something I will not recommend using as it is not used in the Industry. Let and Const are the ones which you will be using.


Getting strong with the fundamentals is the only path to Glory. Wish you very all the Best, Keep Practicing, Peace!

Joey & Chandler

Joey & Chandler

Also published here.

react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!