JavaScriptQuestion 07 of 36
Differences between let, const and var?
Answer out loud or in your head first, then compare.
Answer
var
- function scope,
- can be redeclared,
- its value can change,
- after hoisting it is
undefineduntil assigned.
let
- block scope,
- its value can change,
- can't be redeclared in the same scope.
const
- block scope,
- can't be assigned a new value.
JavaScript
const user = {
name: "Tom"
};
user.name = "Anna";this is fine.
We don't change the reference, only the object's contents.
Rule of thumb:
const → by default
let → when the assignment will change
var → I'd rather not use itHow did it go? Be honest — “Not yet” puts the question on your review list.
Related questions
- B-P08 · How do let, const and var differ? — JavaScript, TypeScript and Node.js