ES6 Flashcards

1
Q

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

A

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:

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.

https://dev.to/sandy8111112004/javascript-introduction-to-scope-function-scope-block-scope-d11

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

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.

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

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 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.
Use const for every other variable.
Do not use var.

https://codeburst.io/javascript-var-let-or-const-which-one-should-you-use-2fd521b050fa

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

What is the syntax for writing a template literal?

A

using backtiks

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

What is “string interpolation”?

A

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.

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

What is destructuring, conceptually?

A

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.

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

What is the syntax for Object destructuring?

A

let { property1: variable1, property2: variable2 } = object;

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

What is the syntax for Array destructuring?

A

let [x, y, z] = getScores();

console. log(x); // 70
console. log(y); // 80
console. log(z); // 90

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

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

A

objects: curly braces
array: brackets

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

What is the syntax for defining an arrow function?

A

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 }

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

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

A

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

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

A

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.

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

What are the three states a Promise can be in?

A

A promise may be in one of 3 possible states: fulfilled, rejected, or pending.

Promise users can attach callbacks to handle the fulfilled value or the reason for rejection.

17
Q

How do you handle the fulfillment of a Promise?

A

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

18
Q

How do you handle the rejection of a Promise?

A

The catch() method returns a Promise and deals with rejected cases only. It behaves the same as calling Promise.prototype.then(undefined, onRejected) (in fact, calling obj.catch(onRejected) internally calls obj.then(undefined, onRejected)). This means that you have to provide an onRejected function even if you want to fall back to an undefined result value - for example obj.catch(() => {}).

19
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.

20
Q

What is the typeof an ES6 class?

A
object ?
Class − A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object.
21
Q

Describe ES6 class syntax.

A
class Polygon { 
   constructor(height, width) { 
      this.height = height; 
      this.width = width; 
   } 
}
22
Q

What is “refactoring”?

A

code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior.

23
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.

https://blog.logrocket.com/commonjs-vs-es-modules-node-js/#:~:text=ES%20modules%20are%20the%20standard,encapsulating%20JavaScript%20code%20for%20reuse.

24
Q

What kind of modules can Webpack support?

A

Webpack supports the following module types natively: ECMAScript modules. CommonJS modules. AMD modules.