Junior — Middle
Explain the differences between the keywords let and const in JavaScript.
sobes.tech AI
Answer from AI
let and const are keywords for declaring variables in JavaScript with block scope.
-
let: declares a variable whose value can be changed after initialization.
-
const: declares a constant whose value cannot be reassigned after initialization.
Important:
constdoes not make an object or array immutable, it only prevents reassignment of the variable itself.
Example:
let a = 5;
a = 10; // allowed
const b = 3;
// b = 4; // error
const obj = { x: 1 };
obj.x = 2; // allowed, property of object is changed
// obj = {}; // error
Thus, let is used for variables that can change, and const for values that should not be reassigned.