ES6 Flashcards

1
Q
  1. What is a code block? What are some examples of a code block?
A

JavaScript statements can be grouped together in code blocks, inside curly brackets {…}.
The purpose of code blocks is to define statements to be executed together.
One place you will find statements grouped together in blocks, is in JavaScript functions:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. What does block scope mean?
A

You can’t access variables defined within the block OUTSIDE the block.

Block Level Scope: This scope restricts the variable that is declared inside a specific block, from access by the outside of the block. The let & const keyword facilitates the variables to be block scoped. In order to access the variables of that specific block, we need to create an object for it. Variables declared with the var keyword, do not have block scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. What is the scope of a variable declared with const or let?
A

In order to access the variables of a specific block, we need to create an object for it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. What is the difference between let and const?
A

let is mutable
let CAN be updated but cannot be re-declared into the scope.
let CAN be declared without initialization.
let can NOT be accessed without initialization as it returns an error.

const is read only
const – can NOT be updated or redeclare into the scope. (It is immutable)
const can NOT be declared without initialization.
const can NOT be accessed without initialization, SINCE it cannot be declared without initialization.
YOU CANNOT REASSIGN A CONST VARIABLE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Why is it possible to .push() a new value into a const variable that points to an Array?
A

When you’re adding to an array or object you’re not re-assigning or re-declaring the constant. It is already declared and assigned, you’re just adding to the “list” that the constant points to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. How should you decide on which type of declaration to use?
A

If the variable is going to be re-assigned at any time, use let
If the variable is constant, use CONST.

Prefer CONST – use const until you can’t.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. What is the syntax for writing a template literal?
A

Define a variable
Use the back tick ‘ ` ‘ to input your data
End with a semicolon

${} (dollar sign and curly braces.
READ: JavaScript expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. What is “string interpolation”?
A

Creating strings by using substitution of placeholders ( ${} )
JavaScript string interpolation is the process of embedding an expression into part of a string. A template literal is used to embed expressions. You can add values such as variables and mathematical calculations into a string using interpolation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. What is destructuring, conceptually?
A

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
(Explode out properties into variables and use them right away – Tim)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What is the syntax for Object destructuring?
A

let { property1: variable1, property2: variable2 } = object;
keyword (var, let, or const)
for objects use curly braces.
And within the curly braces, the property (key name) separated by a colon if you want another name (alias)
Followed by a closing curly brace
Assignment operator (=)
Object you are destructuring properties from.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What is the syntax for Array destructuring?
A

const [ first, second, third… elements in the array] = theArray

THIS IS WRONG  const foo = [‘one’, ‘two’, ‘three’];

const [red, yellow, green] = foo;

console. log(red); // “one”
console. log(yellow); // “two”
console. log(green); // “three”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. How can you tell the difference between destructuring and creating Object/Array literals?
A

Dead giveaway – which side of the brackets show up of the equal sign.
Left side is destructuring
Right side is object literal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. What is the syntax for defining an arrow function?
A

(param1, param2) => {
return;
};

Another way…
param => expression;

Another way…
() => expression;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. When an arrow function’s body is left without curly braces, what changes in its functionality?
A

It is evaluated as an expression rather than a statement.

Ex: const add = (x, y) => x + y
If no curly braces, …
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. How is the value of this determined within an arrow function?
A

It takes the “this” from the enclosing function.
In a normal function, “this” is called during call time.
In arrow functions, “this” is called when the function is DEFINED and it gets its value from the enclosing function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. What are the three states a Promise can be in?
A

a. pending: initial state, neither fulfilled nor rejected.
b. fulfilled: meaning that the operation was completed successfully.
c. rejected: meaning that the operation failed.

17
Q
  1. How do you handle the fulfillment of a Promise?
A

a. Use the “then” and the callback function.

18
Q
  1. How do you handle the rejection of a Promise?
A

a. Use the “catch” method
b. You pass the rejection callback handler.
c. Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.