es6 Flashcards
What is a code block? What are some examples of a code block?
Code blocks are chunks of code within two curly brackets { }
Many things use { }, such as loops, if else statements, etc
What does block scope mean?
The value exists within the scope of the code block
Afterward, the value is returned to its original value
What is the scope of a variable declared with const or let?
block-scoped
What is the difference between let and const?
let allows you to change the value of a variable within a code block
const keeps the value and the it can never be changed
Unless it is referring to properties inside of an object
Why is it possible to .push() a new value into a const variable that points to an Array?
You can edit the items inside a const array. But the object itself, can never not be an array
We are not changing the name binding. We are not changing where the items points
How should you decide on which type of declaration to use?
const should be used for items that will not be edited later
let is used when you know you will be changing the value of an item later on
What is the syntax for writing a template literal?
const bio = My name is ${firstName} ${lastName} and I am ${age} years old.
;
What is “string interpolation”?
Defining a string using template literal and plugging in javascript expressions ( ${ } )
The ability to embed variables and expressions into a string you would concatenated
What is destructuring, conceptually?
Being able to pull the values of an object or array, in such a way that would now require us to refer to the data’s container later on.
What is the syntax for Object destructuring?
First you have the object literal: const book1 = { title: 'Goodnight Punpun', author: 'Inio Asano', libraryID: 3353 };
Then you can deconstruct the object:
const { title, author, libraryID } = book1;
You can now use the values freely:
console.log(The title of this book is ${title}, the author is ${author}, and the library id is ${libraryID}
);
What is the syntax for Array destructuring?
You have your regular array: const numbers = ['one', 'two', 'three', 'four'];
You deconstruct it:
const [first, second, …remainder] = numbers;
Use each of those elements freely:
console. log(first); // “one”
console. log(second); // “two”
console. log(remainder); //this will console [“three”, “four”]
How can you tell the difference between destructuring and creating Object/Array literals?
When creating objects/arrays, the name of the object is on the left side of the = sign:
const book1 = {title: 'Goodnight Punpun', author: 'Inio Asano', libraryID: 335 }; const numbers = ['one', 'two', 'three', 'four'];
When deconstructing them, the properties or list are on the left side of the = sign:
const { title, author, libraryID } = book1;
const [first, second, …remainder] = numbers;
What is the syntax for defining an arrow function?
let functionName = (argument1, argumentN) => { return stuff; }
When an arrow function’s body is left without curly braces, what changes in its functionality?
Leaving out the curly braces, means you have an implied “return”
As soon as you include curly braces, you need to add “return”
How is the value of THIS determined within an arrow function?
The value of THIS is determined at definition time, not call time
In other words, it doesn’t have it’s own THIS, but gets the THIS location of the function it is being called from (aka the object containing the method, which uses the method)