Statements & Declarations Flashcards
What is the scope of a variable declared with var?
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.
What happens if you assign a value to an undeclared variable in non-strict mode?
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.
What happens if you assign a value to an undeclared variable in strict mode?
It will throw a ReferenceError.
What does it mean if a variable is declared with const?
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.
What happens when you modify the property of a const?
const car = { color: 'green' }; car.color = 'red';
This is ok because const doesn’t prevent properties from being altered. Const prevents changing through re-assignment of the const variable.
What happens here with this const?
const car = { color: 'green' }; car = { color: 'blue' };
This is a TypeError because you can’t re-assign the const variable.
Name and describe four types of ‘for’ loops.
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.
Is it preferable to use var or let when declaring the index variable of a vanilla for loop?
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); }
What’s the difference between for…in loops and for…of loops?
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.
Why shouldn’t you use for…in loops on arrays?
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.
What is the result of this code?
function add(x, y) { return x + y; } add(1, 2);
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 }
What is the scope of variable declared with ‘var’ vs ‘let’?
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.
When should you use ‘const’, ‘let’, or ‘var’ for declaring variables?
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.
What is hoisting?
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.
What happens if you try to use a ‘let’ or ‘const’ variable before it is declared?
console.log(foo); let foo = 'bar';
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’.