DEV Community

Cover image for Difference between var, let and const keywords in JavaScript
RADHAKRISHNAN S
RADHAKRISHNAN S

Posted on

Difference between var, let and const keywords in JavaScript

var: Declares variables with function or global scope and
allow re-declaration and updates within the same scope

let: Declares variables with block scope, allowing updates but not re-declaration within the same block.

const: Declares block-scoped variables that cannot be reassigned after their initial assignment.

// var example
var x = 10;
var x = 20;   // Re-declaration allowed
x = 30;       // Update allowed
console.log(x); // Output: 30


// let example
let y = 10;
// let y = 20; // Re-declaration NOT allowed
y = 25;       // Update allowed
console.log(y); // Output: 25


// const example
const z = 10;
// z = 20;     // Re-assignment NOT allowed
console.log(z); // Output: 10
Enter fullscreen mode Exit fullscreen mode

Reference from:https://www.geeksforgeeks.org/javascript/difference-between-var-let-and-const-keywords-in-javascript/

Top comments (0)