es6-const-let Flashcards
What is a code block? What are some examples of a code block?
A code block is a block of code surrounded by curly braces ( { } ). An example of a code block is the code surrounded by curly braces after an “if” statement declaration. Another is a “for” loop declaration.
- The code inside the curly braces of an object is not a code block
What does block scope mean?
Block scope means that variables declared inside a block is not visible to code outside of the block.
What is the scope of a variable declared with const or let?
Block scope
What is the difference between let and const?
A variable declared with the const keyword can’t be re-assigned. Variables declared with the let keyword are mutable while variables declared with the const keyword are immutable.
Why is it possible to .push() a new value into a const variable that points to an Array?
Because even though the variable is declared with the const keyword, you can still change the value of the array the const variable points to. You can’t reassign the const variable to another array.
How should you decide on which type of declaration to use?
If you want to change the value(s) of a variable, declare it with the let keyword. If you don’t want the variable to be reassigned to a different value, declare it with the const keyword.