es6 Flashcards
What is a code block? What are some examples?
a block of code contained by curly braces - loops, if conditions, functions
What does block scope mean?
- the block scoped variable is limited in scope to the block in which it was defined. example:
let x = 10;
if (x = 10) {
let x = 20;
console.log(x); // 20: reference x inside the block
}
console.log(x); // 10: reference x at the beginning of the script
What is the scope of a variable declared with const or let?
block-scoped
What is the difference between let and const?
variables declared by const keyword cannot be reassigned
Why is it possible to .push( ) a new value into a const variable that points to an array?
The const variable is immutable but the values within the array aren’t immutable
How should you decide on which type of declaration to use? (const vs let)
- if the variable will be reassigned, use let
- if the variable is never reassigned, use const
What is the syntax for writing template literals?
let example = ` this is a template literal `;
What is “string interpolation”?
the ability to substitute part of the string for the values of variables or expressions
- (the big difference between template literals and normal strings is substitution. string interpolation allows you to embed variables and expressions in a string. JS will automatically replace these variables/expressions with their values)
what is destructuring, conceptually?
makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
What is the syntax for object destructuring?
const {property1: variable1, property2: variable2,,} = objectName
What is the syntax for array destructuring?
let [element1, element2, element3,,] = arrayName
How can you tell the difference between destructuring and creating object/array literals?
- brackets on the left side of = , destructuring
- if creating object/array, brackets are on the right side of the =
What is the syntax for defining an arrow function?
const funcName = (parameter1, parameter2,…) => { code }
When an arrow functions body is left without curly braces, what changes in its functionality?
- nothing changes if you use a single expression in the code block.
- if you use a statement or multiple lines of code, braces are needed
(arrow functions dont magically know what or when to return)
How is the value of this determined within an arrow function?
- an arrow function captures the this value of the enclosing context
- arrow functions establish “this” based on the scope the arrow function is defined within
- determined at definition time instead of call time
What are the three states a Promise can be in?
- pending : initial state, neither fulfilled or rejected
- fulfilled : meaning that the operation was completed successfully
- rejected : meaning that the operation failed
How do you handle fulfillment of a Promise?
- use the ‘.then’ method
How do you handle the rejection of a Promise?
- use the ‘.catch’ method
What is “syntactic sugar”?
- syntax within a programming language that is designed to make things easier to read or to express. makes the language “sweeter” for human use
Describe ES6 class syntax:
- declare a class followed by a { class body}
- define 0 or more prototype methods of the class
class Name { constructor(param1, param2,..) {
Prototype method( ) {}
}
What is refactoring?
- restructuring existing code without changing its external behavior
- to improve the design, structure, and implementation of the software while preserving its functionality
How are ES6 modules different from CommonJS modules?
- ES6 module syntax is part of the language, while CommonJS is a community “bolt-on”
- different syntax (imports/exports instead of require)
- import/exports are static rather than dynamic
What kind of modules can Webpack support?
- EcmaScript Modules (ES6)
- CommonJS Modules
- AMD Mdules
- WebAssembly Modules