ES6 Flashcards
What is a code block? What are some examples of a code block?
a section of a code that are denoted by curly braces
ex: for, do while, if else, while, try catch each has their own code block
What does block scope mean?
a block scope is code within the curly braces
ex: let and const are block scope
What is the scope of a variable declared with const or let?
block scope (the values of const and let variable is within the block scope where they are declared in)
What is the difference between let and const?
let:
-variables declared by the let keyword are mutable– meaning you reassign their values anytime you want
const:
- immutable– you can’t reassign them to different values (if you do try to reassign, you get TypeError error)
- must immediately initialized to a value (if not, you get SyntaxError error)
- the const keyword ensures that the variable it create is READ-ONLY (BUT it doesn’t mean that the actual value to which the const variable REFERENCE is immutable, meaning you can change the value of its property)
both:
- block-scoped
- have TDZ (temporal death zones)
Why is it possible to .push() a new value into a const variable that points to an Array?
you can add to (the list or elements) of the array that the constant points to/references to, you just can’t reassign or redeclare the constant of that array
when you declare with const, it doesn’t make the content in the array immutable, it makes the name of variable/ the assignment immutable
How should you decide on which type of declaration to use?
if you don’t intend to modify a variable, use const keyword
a good convention to know: use let when we may need to reassign
What is the syntax for writing a template literal?
` ` (backticks) for string
${ } for expressions and variables
What is “string interpolation”?
the ability to substitute part of the string for the values of variables or expressions
What is destructuring, conceptually?
process of breaking up a structure
in our case, the structure is objects and arrays
What is the syntax for Object destructuring?
var declaration keyword, curly braces, property keys, optional aliases, assignment operator, object you’re destructuring
What is the syntax for Array destructuring?
variable keyword, open bracket, element names, closing bracket, assignment operator, array you’re destruring
How can you tell the difference between destructuring and creating Object/Array literals?
the brackets/braces will be on the left for destructuring
and the name of the object or array you’re destructuring will be on the right
What is the syntax for defining an arrow function?
variable declaration keyword, follow by the name of the function, an equal operator, the function keyword, open and close parenthesis with optional parameters inside, follow by an optional pair of curly braces if your code is more than a line, and your code statement inside
When an arrow function’s body is left without curly braces, what changes in its functionality?
you only omit the curly braces if you use an expression in the body of an arrow function (meaning your function is only one line)
so without the curly braces, you don’t need to specify the return keyword, the function will just return the result of the expression
How is the value of this determined within an arrow function?
in an arrow function, it captures the value of THIS of the enclosing context/ scope instead of creating its own this context