JS ES6 Flashcards
What is a code block? What are some examples of a code block?
{ }
code in curly brace
What does block scope mean?
scope of variables is only within curly braces
exist within the block and only affect code within the block; can see out but cannot see in
What is the scope of a variable declared with const or let?
block scoped
What is the difference between let and const?
let: mutable variable
const: immutable variable, must be initialized with value, cannot use in imperative for loop (only for…of loop)
Why is it possible to .push() a new value into a const variable that points to an Array?
You can modify the values of properties, but not reassign a whole new array to a const variable (can’t change memory address of the array);
How should you decide on which type of variable declaration to use?
Will the variable need to be changed within the code block?
Try to use const for everything. change to let if needed
use “const” if ARRAY, OBJECT
use “let” if LOOPS where variable is being modified
What is the syntax for writing a template literal?
string ${variable}
backticks create template literals
${ } for substitutions
What is “string interpolation”?
Using backticks and can substitute part of a string with values of variables or expressions
What is destructuring, conceptually?
destructure individual properties of an object or elements of an array into individual variables.
What is the syntax for Object destructuring?
const { property1: variable1, property2: variable2 } = object;
What is the syntax for Array destructuring?
const [x, y ,…args] = arrayName
How can you tell the difference between destructuring and creating Object/Array literals?
the brackets are on the other side
creating = right side of equal sign
destructuring = left side of equal sign
What is the syntax for defining an arrow function?
const add() => { }
const add = (x, y) => x + y;
OR BLOCK SYNTAX const add = (x, y) => { return x + y; };
When an arrow function’s body is left without curly braces, what changes in its functionality?
without curly brace = [implicit] return, no RETURN keyword needed (anything after arrow will be evaluated and auto returned)
with curly brace = [explicit return], works like regular function code block, need RETURN keyword
How is the value of [this] determined within an arrow function?
captures [this] value of enclosing context