Statements & Declarations Flashcards

1
Q

What is the scope of a variable declared with var?

A

The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.

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

What happens if you assign a value to an undeclared variable in non-strict mode?

A

Assigning a value to an undeclared variable implicitly creates it as a global variable (it becomes a property of the global object) when the assignment is executed.

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

What happens if you assign a value to an undeclared variable in strict mode?

A

It will throw a ReferenceError.

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

What does it mean if a variable is declared with const?

A

The value of a constant can’t be changed through re-assignment. It does not mean the value it holds is immutable though. For example, the properties of a const object can still be altered.

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

What happens when you modify the property of a const?

const car = { color: 'green' };
car.color = 'red';
A

This is ok because const doesn’t prevent properties from being altered. Const prevents changing through re-assignment of the const variable.

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

What happens here with this const?

const car = { color: 'green' };
car = { color: 'blue' };
A

This is a TypeError because you can’t re-assign the const variable.

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

Name and describe four types of ‘for’ loops.

A

1) Vanilla for loop.
2) Array.prototype.forEach: Unable to break out of this. Useful when you need to iterate over all items and have some side effects
3) for…in: iterates over all the enumerable properties of an object. Most practically used for debugging.
4) for…of: iterates over iterable objects like Array, Map, Set, etc. Some people (including AirBnb style guide) suggest avoiding this because it doesn’t always transpile very well.

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

Is it preferable to use var or let when declaring the index variable of a vanilla for loop?

A

Better to use let instead of var for the index so that its scope is limited to the block.

for (let i = 0; i < 10; i++) {
  console.log(i);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s the difference between for…in loops and for…of loops?

A

for. ..in: iterates over all the enumerable properties of an object. Most practically used for debugging.
for. ..of: iterates over iterable objects like Array, Map, Set, etc. Some people (including AirBnb style guide) suggest avoiding this because it doesn’t always transpile very well.

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

Why shouldn’t you use for…in loops on arrays?

A

Should not be used on arrays because it won’t necessarily return the elements in order and it will also return enumerable properties as well as the elements.

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

What is the result of this code?

function add(x, y) {
  return
  x + y;
}
add(1, 2);
A

It returns undefined. The reason is that return is affected by Automatic Semicolon Insertion. No line terminator is allowed between the return keyword and the expression. The code in the question is transformed to

function add(x, y) {
  return;
  x + y; // unreachable
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the scope of variable declared with ‘var’ vs ‘let’?

A

The scope of variables declared with var is the current execution context. This is either the enclosing function or, for variables declared outside any function, global.

Variable declared with ‘let’ are block-scoped, not function-scoped.

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

When should you use ‘const’, ‘let’, or ‘var’ for declaring variables?

A

Most popular opinion is to always use const unless you know the variable is going to change. If it will need to change (like in a for loop) then use let. You shouldn’t ever have to use var again.

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

What is hoisting?

A

Hoisting is a JavaScript mechanism that makes it look like variables and function declarations are getting moved to the top of their scope before code execution. However, what is actually happening is that your function and variable declarations are added to memory during the compile phase.

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

What happens if you try to use a ‘let’ or ‘const’ variable before it is declared?

console.log(foo);
let foo = 'bar';
A

You will get a ReferenceError. Variables declared with let and const remain uninitialized at the beginning of execution while variables declared with var are initialized with a value of ‘undefined’.

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

What is a non-stylistic reason for the convention of placing an opening curly brace at the end of a line rather than on the beginning of a new one?

A

When a line containing the return statement with nothing else on the line is encountered, a semicolon is automatically inserted after the return statement

This function returns undefined because the brace is on a new line.

function foo2() {
  return
  {
    bar: 'hello'
  };
}