What's the difference between var, let and const in JavaScript?
Suggested answer
varis function-scoped, is hoisted (the declaration is moved to the top of the function with the valueundefined) and can be declared again.letis block-scoped{ }, can't be declared again in the same block, and can't be used before the line that declares it.constis likeletbut can't be reassigned. Note: an object or array declared withconstcan still be changed inside.
const user = { name: "Alex" };
user.name = "Sam"; // fine
user = {}; // TypeErrorA good habit: use const by default, use let only when you really need to reassign, and avoid var in new code.