Section 15: ES2015 Part I Flashcards

1
Q

Define ES2015

A

Ecma Script 2015. It comes from a eruopean org

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

const

A
  • Not able to change the value of a primitive but it can mutate if it is an object, but not declared again.
  • Valuable for declaring variables that you don’t want to change.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

let

A
  • Creates ‘block scope’ inside of if, for, while, do, try, catch.
  • When a variable is declared with ‘var’ it gets hoisted to the top of the scope in which it’s in. ‘let’ is in a Temporal Dead Zone (TDZ) so the values can’t be accessed.
  • You cannot redefine a variable using the ‘let’ keyword.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Temporal Dead Zone (TDZ)

A

This is when you don’t have access to a variable because it hasn’t been run yet.

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

Template Strings

A

Build strings much easier:

Instead of building them like this:
console.log(“Hello” + firstName + “ “ + lastName);

You build them like this:
console.log(Hello ${firstName} ${lastName});

*You can also build multiline strings.

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

Arrow Functions

A

*instead of writing ‘function’ you add the arguments first and then ‘=>’.

From: 
var add = function(a,b) {
      return a+b;
}
To: 
var add = (a,b) => {
      return a+b;
}
To one line:
var add = (a,b) => a+b;
  • If you only have a single parameter given to a function, it doesn’t need to wrapped in parenthesis.
  • Arrow functions do not get their own ‘this’ keyword.
  • Inside of an arrow function, the keyword ‘this’ has its original meaning from the enclosing context.
  • Arrow functions do not get their own keyword ‘arguments’
  • Arrow functions should never be used as methods in objects since we will get the incorrect value of the keyword ‘this’.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

define method

A

a function that is placed on an object.

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

Default Parameters

A

Setting a parameter to be a default if nothing is passed in.

Example: 
function add(a=10, b=20){
    return a+b;
}

add(): // 30
add(20); // 40

**These can help reduce conditional logic and confusion.

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

‘For … of’ loop

A

var arr = [1,2,3,4,5];

for(let val of arr){
      console.log(val);
}
// 1
// 2
// 3
// 4
// 5

*Can’t use it on objects

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

Rest operator ‘…’

A
function printRest(a,b,...c){
     console.log(a);
     console.log(b);
     console.log(c);
}

printRest(1,2,3,4,5)

// 1
// 2
// [3,4,5]

*It returns the rest of the elements in an array.

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

Spread Operator ‘…’

A

** When ‘…’ is used outside of parameters in a function.

Examples 1:

var arr1 = [1,2,3];
var arr2 = [4,5,6];
var arr3 = [7,8,9];

var combined = […arr1, …arr2, …arr3];

Example 2:

function sumValues(a,b,c) {
    return a+b+c;
}

var nums = [12,15,20];

sumValues(…nums); // 47

** Basically it spreads the numbers out into separate arguments.

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

Object Shorthand Notation

A

*If there are variables already declared outside of the object, with values, then you don’t need to redeclare them inside of an object.

var firstName = "Elie";
var lastName = "Schoppik";

var instructor = {
firstName,
lastName
}

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

Object Methods

A
var instructor = {
    sayHello() {
         return" Hello!";
     }
}

** Notice we haven’t declared a ‘function’ nor have we used an arrow function. But rather we simply add in the ‘()’

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

Destructuring

A
  • Destructuring allows us to extract values from arrays or properties from objects into distinct variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Object Destructuring

A

//Example 1

var instructor = {
   firstName: "Elie",
   lastName: "Schoppik"
}

var {firstName, lastName} = instructor;

firstName; // “Elie”
lastName; // “Schoppik”

// Example 2
var instructor = {
   firstName: "Elie",
   lastName: "Schoppik"
}

var {firstName:first , lastName: last} = instructor;

first; // “Elie”
last; // “Schoppik”

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

Array Destructuring

A

//Example 1

var arr = [1,2,3]

var [a,b,c] = arr;

a; // 1
b; // 2
c; // 3

17
Q

Swapping Values w/ Array Destructuring

A
function swap(a,b){
   return [a,b] = [b,a]
}

swap(10,5); // [5,10]