JavaScript Variable Declaration and Scope Flashcards

Learn about the different ways to declare variables and scope in JavaScript

1
Q

What are the three ways to declare variables in JavaScript?

A

The three ways to declare variables are:
- var
- let
- const

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

What does var do?

A

Var declares a variable, optionally initialising it to a value.

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

What does let do?

A

Let declares a block scoped variable, optionally initialising it to a value.

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

What does const do?

A

Const declares a block-scoped, read-only name constant.

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

What are the scopes that variables can belong to?

A

The scopes that variables can belong to are:
- Global scope
- Module scope
- Function scope

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

What additional scope can variables declared with let or const belong to?

A

Variables declared with let or const can belong to the block scope.

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

What is global scope?

A

Global scope is the default scope for all code running in script mode.

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

What is module scope?

A

Module scope is the scope for running code in module mode.

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

What is function scope?

A

Function scope is the scope created with a function.

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

What is block scope?

A

Block scope is the scope created with a pair of curly brackets.

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

Where can global variables be accessed?

A

Global variables can be accessed anywhere in the document.

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

Where can local variables be accessed?

A

Local variables can be accessed inside the function they were defined.

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

What does variable hoisting mean?

A

Variable hoisting means that you can refer to the variable anywhere in its scope, even if its declaration isn’t reached yet.

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

What declaration type do you need to use for variable hoisting?

A

You need to use var if you want to do variable hoisting.

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

What happens if you access a hoisted variable before it is declared?

A

If you access a hoisted variable before it is declared its value will be undefined because only its declaration and default initialisation will be hoisted.

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

Where should all var statements be placed in a function?

A

As close as possible to the top.

17
Q

How is hoisting var declarations different from hoisting function declarations?

A

Var declarations only host the declaration but not the value, function declarations are hosted entirely.

18
Q

Can you change a constants value through assignment?

19
Q

Can you re-declare a constant while the script is running?

20
Q

Does const prevent mutations?

A

No you are able to mutate constants.