Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Latest commit

 

History

History
312 lines (216 loc) · 5.64 KB

02-es6.md

File metadata and controls

312 lines (216 loc) · 5.64 KB

Can you overwrite var declarations i.e. can you declare the same variable multiple times using var?

Yes! This does not throw an error:

var camper = 'James';
var camper = 'David';

What are the possible scopes for var?

  • global
  • local in function

What are the possible scopes for let?

  • global
  • function
  • block
  • statement
  • expression

What is the scope of the i keyword in a typical for loop for (var i = 0; i < 3; i++) {}?

Global

How many i variables will this for loop create: for (let i = 0; i < 3; i++) {}?

Three different i variables with unique values (0, 1, and 2).

How do I prevent an object from being mutated?

Object.freeze(obj)

Open question: can arrays be frozen too?

What is the most concise form of an arrow function?

const myFunction = () => returnValue;

What is the most concise form of an arrow function with a single param?

const doubler = item => item * 2;

How do I provide defaults to function params?

const greeting = (name = "Anonymous") => "Hello " + name;

How do I allow a function to accept a variable number of arguments?

function howMany(...args) {
  return "You have passed " + args.length + " arguments.";
}

How can I expand arrays and other expressions in places where multiple parameters or elements are expected?

// Math.max expects comma-separated arguments
Math.max(...arr)

Can I assign unpacked array to a variable e.g. const spreaded = ...arr;?

No, ... works only in place. The assignment above will throw an error.

How do I clone an array using the spread operator?

[...arr]

What is a destructuring assignment?

const user = { name: 'John Doe', age: 34 };

const { name, age } = user;

How do I use new variable names when extracting values from an object using the destructuring assignment?

const { name: userName, age: userAge } = user;

How do I use destructuring assignment to extract nested values from an object?

const user = {
  johnDoe: {
    age: 34,
    email: 'johnDoe@freeCodeCamp.com'
  }
};

const { johnDoe: { age, email }} = user;

How do I use Destructuring Assignment to Assign Variables from Arrays?

const [a, b] = [1, 2, 3, 4, 5, 6];
console.log(a, b); // 1, 2

How do I Destructuring Assignment to Assign Variables from Arrays with specific indices?

const [a, b,,, c] = [1, 2, 3, 4, 5, 6];

How do I swap values using destructuring assignment?

let a = 8, b = 6;

[a, b] = [b, a];

How do I use the rest parameter to collect the rest of the elements into a separate array?

const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
console.log(a, b); // 1, 2
console.log(arr); // [3, 4, 5, 7]

How do I Use Destructuring Assignment to Pass an Object as a Function's Parameters?

const profileUpdate = ({ name, age, nationality, location }) => {
  /* do something with these fields */
}

How do I use Object Property Shorthand?

const getMousePosition = (x, y) => ({ x, y }); // returns { x: x, y: y }

How do I write Concise Declarative Functions in ES6?

const person = {
  name: "Taylor",

  sayHello() {
    return `Hello! My name is ${this.name}.`;
  }
};

How do I instantiate an object in ES5?

var SpaceShuttle = function(targetPlanet){
  this.targetPlanet = targetPlanet;
}

var zeus = new SpaceShuttle('Jupiter');

How do I define a class in ES6?

class SpaceShuttle {
  constructor(targetPlanet) {
    this.targetPlanet = targetPlanet;
  }
}

const zeus = new SpaceShuttle('Jupiter');

What does the class keyword actually return?

A function.

How do I create getter and setter functions in ES6?

class Book {
  constructor(author) {
    this._author = author;
  }

  // getter
  get writer() {
    return this._author;
  }

  // setter
  set writer(updatedAuthor) {
    this._author = updatedAuthor;
  }
}

const lol = new Book('anonymous');
console.log(lol.writer);  // anonymous

lol.writer = 'wut';
console.log(lol.writer);  // wut

What is the convention for naming private variables?

    this._author = author;

How do I declare a script such that it can use export and import features?

<script type="module" src="filename.js"></script>

How can I export a single function?

export const add = (x, y) => {
  return x + y;
}

How do I export multiple functions?

export { add, subtract };

How do I import functions?

import { add, subtract } from './math_functions.js';

How do I import everything from a file?

import * as myMathModule from "./math_functions.js";

Question: does import * import only exported variables?

Question: how does one use a fallback value for a module or file?

How do I specify a function as the fallback value of a module?

export default function add(x, y) {
  return x + y;
}

How do I import the default export of a module?

import add from "./math_functions.js";

How do I create a promise?

const makeServerRequest = new Promise((resolve, reject) => {

});

How do I resolve/reject to make the promise succeed/fail?

const makeServerRequest = new Promise((resolve, reject) => {
  // responseFromServer represents a response from a server
  let responseFromServer;

  if(responseFromServer) {
    resolve("We got the data");
  } else {
    reject("Data not received");
  }
});

What are the states of a promise?

  • pending
  • fulfilled
  • rejected

How do I handle a fulfilled promise?

myPromise.then(result => {
  // do something with the result.
});

How do I handle a rejected promise?

myPromise.catch(result => {
  // do something with the result.
});