Section 15: ES2015 Part I Flashcards
Define ES2015
Ecma Script 2015. It comes from a eruopean org
const
- 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.
let
- 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.
Temporal Dead Zone (TDZ)
This is when you don’t have access to a variable because it hasn’t been run yet.
Template Strings
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.
Arrow Functions
*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’.
define method
a function that is placed on an object.
Default Parameters
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.
‘For … of’ loop
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
Rest operator ‘…’
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.
Spread Operator ‘…’
** 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.
Object Shorthand Notation
*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
}
Object Methods
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 ‘()’
Destructuring
- Destructuring allows us to extract values from arrays or properties from objects into distinct variables.
Object Destructuring
//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”