A JavaScript variable is a container used to store data values such as numbers, strings, booleans, objects, and more. You declare variables using var, let, or const.
var: Function-scoped, older way of declaring variables.let: Block-scoped variable that can be reassigned.const: Block-scoped constant that cannot be reassigned.
var x = 5;
let y = 10;
const z = 15;
let firstName = "John";
let _age = 30;
let $salary = 5000;
let score = 50;
score = 75; // Valid
const pi = 3.14;
pi = 3.14159; // ❌ Error - Assignment to constant variable
let a = 1, b = 2, c = 3;
let score = 10;
console.log("Initial score:", score);
score = 20;
console.log("Updated score:", score);
const pi = 3.14;
console.log("Value of pi:", pi);
Initial score: 10 Updated score: 20 Value of pi: 3.14
Click the button to increase the counter.
Current counter value: 0
let for values that change.const for fixed values.var in modern code.const city = "Pune";) and attempt to reassign it. Check the console for the error.