es6 Flashcards
What is a code block? What are some examples of a code block?
A block of code within the curly braces. Function, while, for, if else.
What does block scope mean?
An area within the block where variables can be referenced.
Variables are only accessible within the block.
What is the scope of a variable declared with const or let
let = block-scoped (within any code block { }. Ex. if (xx) {} for(xx) {})
const = block-scoped
var = function-scoped - if there is no function it is defined in, it’s globally scoped.
What is the difference between let and const?
Const can’t be reassigned. The values of whatever it is assigned to can be manipulated or changed but you cannot reassign it.
Let can be reassigned.
why is it possible to .push() a new value into a const variable that points to an array?
The value with the array is mutable.
How should you decide on which type of variable declaration to use?
If the variable is not going to be reassigned, use ‘const’.
If the variable will be reassigned then use ‘let’.
What is the syntax for writing a template literal?
Template literals use backticks rather than single or double quotes and commonly use string interpolation for JavaScript expressions or variables: ${variable} ${1 + 1}
What is string interpolation?
A process where variables and expressions are embedded in a string. The variable/expression must be placed in a space block as follows: ${variable_name}
What is destructuring conceptually?
Taking the values within an object or an array and assigning them to a variable.
What is the syntax for object destructuring?
let { property1:variable1, property2: variable2} = object
What is the syntax for array destructuring?
let [index1, index2] = array
How can you tell the difference between destructuring and creating object/array literals?
Destructuring: the variable name goes on the right of the assign operator.
let/const {} or [] = variable name
Creating: variable name goes on the left of the assign operator
variable = {} or []
What is the syntax for defining an arrow function?
(parameters separated by commas) => {}
If there is only 1 parameter the parentheses are not needed. If the return is a simple expression, brackets and return keyword can be omitted. Brackets and return keyword are needed for code block if it’s multiline statements.
When an arrow function’s body is left without curly braces, what changes in its functionality?
It becomes a return
How is the value of this determined within an arrow function?
Arrow functions: the value of this
is determined at definition time.
Regular functions: the value of this
is determined at call time.