ES6 Flashcards
What is a code block? What are some examples of a code block?
where code is executed, usually inside curly brackets
What does block scope mean?
global or local, where a variable is accessible. if inside a code block and using let: the block scope is local. If outside of code block or var is used then block scope is global
let and const variables are only accessible inside the code block
What is the scope of a variable declared with const or let?
block-scope, can only be accessed inside the code block if inside a code block
What is the difference between let and const?
let: is mutable–> can be changed and mutated within the scope
const: is immutable –> cannot be changed or reassigned, but can add inside
Why is it possible to .push() a new value into a const variable that points to an Array?
when pushing, you are not reassigning or redeclaring, you are adding to the object so techincally not really “changing” anything
How should you decide on which type of declaration to use?
when variable is not going to be reassigned use const, when it is, then use let
if read only use const
how can you change const variable
you can change const property values but not change the object literal
obj = {a: 2} obj.a = 3
not
obj = {a: 2} obj = {a : 3}
you can also add (push) to const array
What is the syntax for writing a template literal?
back ticks –> some string
white spaces count
can do new line
What is “string interpolation”?
adding variable within template literal –>
const name = 'bob' `my name is ${name}`
What is destructuring, conceptually?
assigning variables in regards to an object/ array structure
assigning it in a ‘backwards’ way
What is the syntax for Object destructuring?
{property1: var1, property2: var2} = object
if var is same name as property name –>
{var1, var2} = obj
What is the syntax for Array destructuring?
[var1, var2, var3] = array
to get leftovers –>
[var1, var2, …leftovers] = array
to skip through first values –>
[, , , var] = array
How can you tell the difference between destructuring and creating Object/Array literals?
the variables are within an object bracket or array bracket and the assignment value is the object/array you are pulling value from
deconstructing objects with null returns from func
deconstructing array with null returns from fun
use OR operator and empty object as falback –>
let { firstName, lastName } = getPerson() || {};
let [a = 10, b = 20] = getItems() || [];
setting default values for array deconstruction
assign value within deconstructor
let [, , thirdItem = 0] = getItems();
What is the syntax for defining an arrow function?
const varName = (parameter) => { codeblock }
When an arrow function’s body is left without curly braces, what changes in its functionality?
right side must have return expression, if statement on right side, must put it inside curly braces
How is the value of this determined within an arrow function?
value of [this] is determined when arrow function is defined
this becomes outside of scope but within function
since arrow function [this] is defined in definition time, when not inside a function, this in an arrow function becomes window
What are the three states a Promise can be in?
How do you handle the fulfillment of a Promise?
How do you handle the rejection of a Promise?
pending, fulfilled, rejected
fulfillment of a promise is with .then( ) and whatever you want to happen
rejection of a promise is with .catch( ) or also.then( )
What is Array.prototype.filter useful for?
to apply a call back function to each element of an array
returns a new array that applies to the condition of individal arrays
in the call back return has to be true or false to decide if it will be returned in the output array
What is Array.prototype.map useful for?
to apply a callback function to each element of an array
What is Array.prototype.reduce useful for?
to stack array values into one value
What is “syntactic sugar”?
What is “refactoring”?
syntactic sugar is when you make a syntax “sweeter” to make things easier to read or express
refactoring is restructuring code without changing its functionality to make it easier to build onto or to read/understand
What is the typeof an ES6 class? Describe ES6 class syntax.
object,
class NameOfClass {
constructor(parameters) { } methods( ) { }
}