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();