Variables Flashcards

1
Q

What is considered a variable name in JS? what is not?

A
  1. Variable names declared by let or var
  2. constant name declared by const
  3. Function names
  4. Function parameters
  5. Class names

Property names of objects are generally considered identifiers but NOT variable names. The exception to this is property names of the global object.

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

What is the return value of an assignment?

A

the right hand side of the ‘=’.
But be careful, declarations, even with assignment, are statements and don’t have return values.

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

How are primitive variables and objects/arrays passed in JavaScript?

A

Primitive variables in JavaScript are passed by value, while arrays and objects are passed by reference.

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

What does it mean when a variable is hoisted in JavaScript?

A

All variables in JavaScript declared with var are hoisted, meaning they are virtually moved to the beginning of the scope.

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

What scope do undeclared variables have?

A

All undeclared variables in JS (created without the use of let or const) have global scope.

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

Can you mutate passed objects within a function in JavaScript?

A

While you can mutate passed objects within a function, reassignment is not a destructive operation. Reassigning a passed object within a function will NOT mutate the passed object. This is because the object within the function is a copy of the pointer to the passed object. If we reassign to this COPY it changes the pointer stored in the copy to point at the new object without changing what the original pointer points at.

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

What is global scope?

A

Any function or code block within your program can read and modify variables with global scope.

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

How is global scope created in JS?

A

Variables declared in global scope are typically defined outside of any functions or code blocks. Variables declared without the use of let or const are also have global scope.

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

What is local Scope?

A

Variables in local scope are typically declared within functions, conditional statements, loops, or other code blocks. They are “local” to that block of code, and they cannot be directly accessed from outside it.

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

How is local and block scope different?

A

Local scope is function-level, meaning it encompasses the entire function, while block scope is limited to the specific block where the variable is declared.

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

What is a block?

A

A block is a related set of JavaScript statements and expressions between a pair of opening and closing curly braces. These include if statements, loops, switch, etc. function bodies are not technically blocks but we can think of them as such for scoping purposes. Object literals are NOT blocks.

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

what is variable shadowing?

A

Variable shadowing occurs when a variable with the same name is declared in an inner scope, such as a function or a block, as a variable in an outer scope. In such cases, the variable in the inner scope shadows or hides the variable in the outer scope.

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

How should you declare variables in JS?

A

variables should be declared with let or const

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