ES6 Flashcards
What is a code block? What are some examples of a code block?
In computer programming, a block or code block or block of code is a lexical structure of source code which is grouped together. Blocks consist of one or more declarations and statements.
In JavaScript, blocks are denoted by curly braces {} , for example, the if else, for, do while, while, try catch and so on:
What does block scope mean?
A block scope is the area within if, switch conditions or for and while loops. Generally speaking, whenever you see {curly brackets}, it is a block. In ES6, const and let keywords allow developers to declare variables in the block scope, which means those variables exist only within the corresponding block.
https://dev.to/sandy8111112004/javascript-introduction-to-scope-function-scope-block-scope-d11
What is the scope of a variable declared with const or let?
block scope
What is the difference between let and const?
const
is a signal that the identifier won’t be reassigned.
let
is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.
https://medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75#:~:text=%60const%60%20is%20a%20signal%20that,always%20the%20entire%20containing%20function.
Why is it possible to .push() a new value into a const variable that points to an Array?
This happens because your constant is actually storing a reference to the array. When you join something into your array you are not modifying your constant value, but the array it points to. The same would happen if you assigned an object to a constant and tried to modify any property of it.
If you want to freeze an array or object so it can’t be modified, you can use the Object.freeze method, which is already part of ECMAScript 5.
https://stackoverflow.com/questions/23436437/why-can-i-change-a-constant-object-in-javascript
How should you decide on which type of declaration to use?
Use let when you know that the value of a variable will change.
Use const for every other variable.
Do not use var.
https://codeburst.io/javascript-var-let-or-const-which-one-should-you-use-2fd521b050fa
What is the syntax for writing a template literal?
using backtiks
What is “string interpolation”?
String interpolation is replacing placeholders with values in a string literal. The string interpolation in JavaScript is performed by template literals (strings wrapped in backticks ` ) and ${expression} as a placeholder.
What is destructuring, conceptually?
Destructuring broadly means extracting data from arrays or objects. Using destructuring, we can break a complex array or object into smaller parts. Destructuring also offers us the ability to extract multiple data at once from an object or array.
What is the syntax for Object destructuring?
let { property1: variable1, property2: variable2 } = object;
What is the syntax for Array destructuring?
let [x, y, z] = getScores();
console. log(x); // 70
console. log(y); // 80
console. log(z); // 90
How can you tell the difference between destructuring and creating Object/Array literals?
objects: curly braces
array: brackets
What is the syntax for defining an arrow function?
An arrow function expression is an anonymous function expression written with the “fat arrow” syntax ( => ). Rewrite the sum function with arrow function syntax: const sum = (a, b) => { return a + b }
When an arrow function’s body is left without curly braces, what changes in its functionality?
The parenthesis are returning a single value, the curly braces are executing multiple lines of code.
with the curly braces, its a statement so you need a ‘return’ keyword.
const add = (x, y) => x+y; //expression.. const add = (x, y) => { return x+y; } //explicitly saying to return
How is the value of this determined within an arrow function?
Arrow functions have a lexical this and its value within the arrow function is determined by the surrounding scope. You probably assume that this gets a different value within the person object literal.