Variables var, let, and const
In JavaScript, variables are used to store data values. There are three ways to declare a variable: var, let, and const.
var – Old way (pre‑ES6). Function‑scoped, can be redeclared, and is hoisted.
var name = "Alice";var name = "Bob"; // redeclaration allowedconsole.log(name); // Boblet – Introduced in ES6. Block‑scoped, cannot be redeclared in the same scope, but can be updated.
let age = 25;age = 26; // allowed
// let age = 30; // Error: Identifier 'age' has already been declaredconst – Also ES6. Block‑scoped, cannot be updated or redeclared. Must be initialized at declaration.
const PI = 3.14159;// PI = 3.14; // Error: Assignment to constant variable// const GRAVITY; // Error: Missing initializer in const declarationFor objects and arrays declared with const, the reference cannot change, but properties/elements can be modified.
const person = { name: "Alice" };person.name = "Bob"; // allowed// person = { name: 'Charlie' }; // ErrorBest practice: Use const by default, let when you need to reassign, and avoid var in modern code.