es6-const-let Flashcards

1
Q

What is a code block? What are some examples of a code block?

A

In JavaScript, blocks are denoted by curly braces {} , for example, the if else, for, do while, while, try catch and so on:

allows you to put multiple statements where there would normally be a single statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does block scope mean?

A

Because the let keyword declares a block-scoped variable, the x variable inside the if block is a new variable and it shadows the x variable declared at the top of the script. Therefore, the value of x in the console is 20.

the scope is the current block statement

whatever the variable is defined as within that block is not accessible outside the code block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the scope of a variable declared with const or let?

A

Like the let keyword, the const keyword declares blocked-scope variables. However, the block-scoped variables declared by the const keyword can’t be reassigned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between let and const?

A

let can be updated but not re-declared. const cannot be updated or re-declared. both are hoisted but not initialized

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is it possible to .push() a new value into a const variable that points to an Array?

A

Then, we can change the array’s elements by adding the green color. However, we cannot reassign the array colors to another array.

Youre not assigning, just manipulating original variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How should you decide on which type of declaration to use?

A

As a general rule, you should always declare variables with const, if you realize that the value of the variable needs to change, go back and change it to let. Use let when you know that the value of a variable will change. Use const for every other variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly