DEV Community

tarekibnkhayer
tarekibnkhayer

Posted on

Declare vs Assign vs Reassign vs Redeclare in JavaScript

Declare: We can declare a variable using the var, let, and const keywords.
Ex: var x; let y; const z = 10; (Using const keyword variable has to be declared and assigned when it is created);

Assign: We can assign a value to a variable using the assignment operator(=).
Ex: x = 5; y = 6;

Reassign: We can reassign a value to a variable except which is declared and assigned with const.
Ex: x = 7; y = 8;
But we declared and assigned const z = 10; before, then we can't reassign it. We can't write the code: z = 4; If we do, JS will throw an error.

Redeclare: We can redeclare a variable if we use var, otherwise not.
Ex: var a; a = 5; var a = 7; // it won't create any problem.
let b; b = 5; let b = 7; // this is not allowed. Using the let keyword variable can't be redeclared.
const c = 5; const c = 8; // this is not allowed. Using the const keyword variable can't be redeclared.

One more thing: Using const keyword variable has to be declared and assigned when it is created. You can't assign the value later.
Ex: const d = 5; ( Declaration and Assignment at the same time);

Top comments (0)