ES6/7 and TypeScript Flashcards

A quick refresher on some of the new es6/7 features

1
Q

What kind of return is this arrow function using?

const mappedPizzas = pizzas.map( pizza => pizza.name.toUppercase() )

A

Implicit return

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

What is unique about arrow functions and the this keyword?

A

Arrow functions don’t have a ‘this’ keyword. Before arrow functions, each function defined its own ‘this’ value.

Which was problematic when, for example, trying to reference a class instance from a function within that class - ‘this’ would refer to the function, not the instance.

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

How do you pass in default function parameters in es6?

A
function multiply(a, b = 25) {
    return a * b;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the es6 shorthand for initialising properties in object literals?

A

In es6 object literals check if a property key matches any variables of the same name. If there is a match, the variable value is automatically assigned to that key.

const brand = 'Apple';
const model = 'Macbook Pro';
const year = 2012;
const laptop = {
    brand,
    model,
    year
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the shorthand for defining methods in objects with es6?

A

You don’t need to define methods with a function keyword or arrow function. Just use parenthesis instead of the colon and then include the function body.

const pizza = {
    name: "Pepperoni"
    getName() {
        return this.name;
    } 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What do es6 rest parameters do?

A

Represent an indefinite number of arguments as an array. This is similar in functionality to the native Javascript arguments object, though allows you to achieve the same results with less boilerplate.

function myFun(a, b, ...manyMoreArgs) {
  console.log("a", a); 
  console.log("b", b);
  console.log("manyMoreArgs", manyMoreArgs); 
}

myFun(“one”, “two”, “three”, “four”, “five”, “six”);

// Console Output:
// a, one
// b, two
// manyMoreArgs, [three, four, five, six]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give an example of using the array spread operator

A

const combinedArray = […arrayOne, …arrayTwo];

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

In what ways are es6 de-structuring of arrays and objects commonly used?

A
  • For arrays:*
  • accessing an arrays items individually
  • For objects:*
  • renaming parameters
  • accessing an objects properties individually
  • this finer control is commonly used to reduce function parameters; you input only those properties you need, as opposed to a whole object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give an example of de-structuring (an object) to input reduced parameters in es6.

A
const pizza = {
    name: 'Pepperoni',
    toppings: ['pepperoni'],
    base: 'traditional'
}
function order({ name, toppings }) {
    console.log(name, topppings)
}

order(pizza)

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

Give an example of using de-structuring (an object) to rename parameters in es6.

A
const pizza = {
    name: 'Pepperoni',
    toppings: ['pepperoni']
}
function order({ name: pizzaType, toppings: pizzaToppings }) {
    console.log(pizzaType, pizzaToppings)
}

const pizzaName = order(pizza)

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

Give an example of using de-structuring (an array) in es6.

A
const toppings = [ 'pepperoni', 'bacon', 'chilli' ];
const [ varOne, varTwo, varThree ] = toppings;
// varOne = 'pepperoni'
// varTwo = 'bacon'
// varThree = 'chilli'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between implicit and explicit types and when would you use either approach?

A

Typescript has the ability to infer types rather than have you explicitly set them.

It’s not necessary to be explicit with your types at all times. For example, you can regularly see the type of simple variables - declaring types here is just boilerplate.

However, for complex types, or if you are declaring an empty variable which is later assigned a value, you will want to assign a type to avoid an ‘any’ typecasting.

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

Explain the void type.

A

It means there is no type at all. You can actually set a variable to the void type but then you can never change it - nulls and undefined are what is generally used here. Voids are normally used to tell typescript you aren’t returning anything from a function.

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

Explain the never type.

A

The never type tells the typescript compiler that a value will never occur. For example, set a function to never if it should never return anything - like an error handler that throws a new error mid function body.

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

What does typescripts strict null check do?

A

The typescript compiler has an option to set strictNullChecks, which forces you to be explicit about undefined and null types. For example, it will error if you try to set null on a string type, unless you explicitly create a union type where the type is string | null.

This prevents unexpected null or undefined errors.

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

Explain union types.

A

Union types are a way to combine types and are commonly used to be more explicit about a type. For example, setting a string type for a size var would accept any string when you may only have four strings you want to accept. In this case, explicitly setting those four strings is much safer:

string

vs

“extraLarge” | “large” | “medium” | “small”