ES6 Flashcards

1
Q

What is a code block? What are some examples of a code block?

A
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
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does block scope mean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the scope of a variable declared with const or let?

A

Block scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between let and const?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is it possible to .push() a new value into a const variable that points to an Array?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How should you decide on which type of declaration to use?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between using ‘var’ and ‘let’?

A

1) “var” is function-scoped, whereas “let” is block-scoped.

2) Using “var” attaches the variable to the window, whereas “let” prevents it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the syntax for writing a template literal?

A
  • 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`;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is “string interpolation”?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is destructuring, conceptually?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the syntax for Object destructuring?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the syntax for Array destructuring?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you tell the difference between destructuring and creating Object/Array literals?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the syntax for defining an arrow function?

A
  1. Remove the word “function” and place arrow between the argument and opening body bracket
  2. Remove the body braces and word “return” — the return is implied.
  3. 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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When an arrow function’s body is left without curly braces, what changes in its functionality?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How is the value of this determined within an arrow function?

A

For arrow function, the value of this is determined when the arrow function is defined(definition time).
The opposite of normal functions (this is determined when the function is called(call time))

17
Q

What are the three states a Promise can be in?

A

Pending, resolved, or rejected

  • One and done
  • Fail or success ONCE
18
Q

How do you handle the fulfillment of a Promise?

A
Use the then() method. 
The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.
19
Q

How do you handle the rejection of a Promise?

A
You use the catch() method.
The catch() method returns a Promise and deals with rejected cases only.
20
Q

What is Array.prototype.filter useful for?

A

The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

21
Q

What is Array.prototype.map useful for?

A

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

22
Q

What is Array.prototype.reduce useful for?

A

The reduce() method executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

23
Q

What is the difference between forEach and map?

A

forEach does not return anything while map does have a return

24
Q

What is “syntactic sugar”?

A

Syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language “sweeter” for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

25
Q

What is the typeof an ES6 class?

A

Functions

26
Q

Describe ES6 class syntax.

A
  1. Class keyword
  2. Define the function name
  3. Code block
  4. Within the code block there is an optional constructor keyword that includes the parameters
  5. Code block that includes the parameters being assign to “this”
  6. Method calls that you would like to be defined.
ex. 
class Person {
    constructor(name) {
        this.name = name;
    }
    getName() {
        return this.name;
    }
}
27
Q

What is “refactoring”?

A

code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior. Refactoring is intended to improve the design, structure, and/or implementation of the software (its non-functional attributes), while preserving its functionality.

28
Q
How to you read: 
class Student {
  constructor(firstName, lastName, subject) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.subject = subject;
  }
A
We have a class being defined name student with an opening curly brace for the class body. 
We have a constructor method being defined with \_\_ parameters and an opening curly brace for the method code block.
29
Q
How do you read:
getFullName() {
    const { firstName, lastName } = this;
    return `${firstName} ${lastName}`;
  }
A

The prototype method getFullname is being defined and an opening curly brace for the method code block.
the “this” variable is being destructured and assigned to the const variable firstName and lastName.

30
Q

How are ES Modules different from CommonJS modules?

A

ES modules are the standard for JavaScript, while CommonJS is the default in Node.js.
The syntax is also different.

31
Q

What kind of modules can Webpack support?

A

ECMAScript modules. CommonJS modules. AMD modules.