ES6 Flashcards
What is a code block? What are some examples of a code block?
In JavaScript, blocks are denoted by curly braces {}. Ex. for example, the if else, for, do while, while, try catch and so on: if (condition) { // inside a code block }
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.
‘var’ is function scoped, while const and let are block scoped.
What is the scope of a variable declared with const or let?
Block scope
What is the difference between let and const?
- Like the let keyword, the const keyword declares blocked-scope variables.
HOWEVER, the block-scoped variables declared by the const keyword can’t be reassigned. - The variables declared by the let keyword are mutable. It means that you can change their values anytime you want.
However, variables created by the const keyword are “immutable”. In other words, you can’t reassign them to different values.
If you attempt to reassign a variable declared by the const keyword, you’ll get a TypeError
The const keyword ensures that the variable it creates is read-only.
Why is it possible to .push() a new value into a const variable that points to an Array?
Even though the const keyword ensures that the variable is read-only, it doesn’t mean that the actual value to which the const variable references is immutable. ex. const person = { age: 20 }; person.age = 30; // OK console.log(person.age); // 30
However, you cannot reassign a different value to the person constant like this:
person = { age: 40 }; // TypeError
How should you decide on which type of declaration to use?
Use let when you know that the value of a variable will change/reassigned. Use const when you know that identifier won’t be reassigned.
Use let
when you need to reassign a variable. You should use one variable to represent one thing, the use case for let
tends to be for loops or mathematical algorithms.
What is the difference between using ‘var’ and ‘let’?
1) “var” is function-scoped, whereas “let” is block-scoped.
2) Using “var” attaches the variable to the window, whereas “let” prevents it.
What is the syntax for writing a template literal?
- Instead of using the single quotes or double quotes, a template literal uses backticks
- Using the backticks, you can freely use the single or double quotes in the template literal without escaping.
ex.
let anotherStr =Here's a template literal
; - If a string contains a backtick, you must escape it using a backslash ()
ex.
let strWithBacktick =Template literals use backticks \
insead of quotes`;
What is “string interpolation”?
- The ability to substitute part of the string for the values of variables or expressions. The JavaScript engine will automatically replace these variables and expressions with their values.
- To instruct JavaScript to substitute a variable and expression, you place the variable and expression in a special block as follows:
${variable_name} - example:
let firstName = ‘John’,
lastName = ‘Doe’;
let greeting = `Hi ${firstName}, ${lastName}`; console.log(greeting); // Hi John, Doe
What is destructuring, conceptually?
Destructuring is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. That is, we can extract data from arrays and objects and assign them to variables.
What is the syntax for Object destructuring?
ex. let person = { firstName: 'John', lastName: 'Doe' }; let { firstName: fname, lastName: lname } = person; - Code language: JavaScript (javascript) In this example, the firstName and lastName properties are assigned to the fName and lName variables respectively.
What is the syntax for Array destructuring?
Ex. const thing = ["Table", "Chair", "Fan"]; const [a, b, c] = thing; console.log(a); // Output: Table console.log(b); //Output: Chair console.log(c); //Output: Fan
- It takes each variable on the array literal on the left-hand side and maps it to the same element at the same index in the array.
How can you tell the difference between destructuring and creating Object/Array literals?
For destructuring, you take an existing variable that you would like to destructure and put it on the right side of the = sign and use object/array syntax after the const variable keyword.
When creating Object/Array literals, you make the object/array on the right side of the equal sign and assign to a unique variable name on the left side of the equal sign.
What is the syntax for defining an arrow function?
- Remove the word “function” and place arrow between the argument and opening body bracket
- Remove the body braces and word “return” — the return is implied.
- Remove the argument parentheses. However, if you have multiple arguments or no arguments, you’ll need to re-introduce parentheses around the arguments
ex.
1. (param1, param2) => {return;};
2. param => exp;
3. () => exp;
When an arrow function’s body is left without curly braces, what changes in its functionality?
- Without curly braces {}. With this syntax, the arrow function has an implicit return.
- With curly braces {}. With this syntax, the arrow function does not have an implicit return.
- The function body gets evaluated as an expression. Implicitly, the arrow function returns that result of that expression.