es6 Flashcards
What is a code block? What are some examples?
a block of code contained by curly braces - loops, if conditions, functions
What does block scope mean?
- the block scoped variable is limited in scope to the block in which it was defined. example:
let x = 10;
if (x = 10) {
let x = 20;
console.log(x); // 20: reference x inside the block
}
console.log(x); // 10: reference x at the beginning of the script
What is the scope of a variable declared with const or let?
block-scoped
What is the difference between let and const?
variables declared by const keyword cannot be reassigned
Why is it possible to .push( ) a new value into a const variable that points to an array?
The const variable is immutable but the values within the array aren’t immutable
How should you decide on which type of declaration to use? (const vs let)
- if the variable will be reassigned, use let
- if the variable is never reassigned, use const
What is the syntax for writing template literals?
let example = ` this is a template literal `;
What is “string interpolation”?
the ability to substitute part of the string for the values of variables or expressions
- (the big difference between template literals and normal strings is substitution. string interpolation allows you to embed variables and expressions in a string. JS will automatically replace these variables/expressions with their values)
what is destructuring, conceptually?
makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
What is the syntax for object destructuring?
const {property1: variable1, property2: variable2,,} = objectName
What is the syntax for array destructuring?
let [element1, element2, element3,,] = arrayName
How can you tell the difference between destructuring and creating object/array literals?
- brackets on the left side of = , destructuring
- if creating object/array, brackets are on the right side of the =
What is the syntax for defining an arrow function?
const funcName = (parameter1, parameter2,…) => { code }
When an arrow functions body is left without curly braces, what changes in its functionality?
- nothing changes if you use a single expression in the code block.
- if you use a statement or multiple lines of code, braces are needed
(arrow functions dont magically know what or when to return)
How is the value of this determined within an arrow function?
- an arrow function captures the this value of the enclosing context
- arrow functions establish “this” based on the scope the arrow function is defined within
- determined at definition time instead of call time