ES6 Cheatsheet

Arrow function

const sum = (a, b) => a + b

console.log(sum(2, 6)) // prints 8

Default parameters

function print(a = 5) {
    console.log(a)
}

print() // prints 5
print(22) // prints 22

let scope

let a = 3

if (true) {
    let a = 5
    console.log(a) // prints 5
}

console.log(a) // prints 3

const

// can be assigned only once:
const a = 55

a = 44 // throws an error

Multiline string

console.log(`
  This is a 
  multiline string
`)

Template strings

const name = 'Leon'
const message = `Hello ${name}`

console.log(message) // prints "Hello Leon"

String includes()

console.log('apple'.includes('pl')) // prints true
console.log('apple'.includes('tt')) // prints false

String startsWith()

console.log('apple'.startsWith('ap')) // prints true
console.log('apple'.startsWith('bb')) // prints false

String repeat()

console.log('ab'.repeat(3)) // prints "ababab"

Destructuring array

let [a, b] = [3, 7];

console.log(a); // 3
console.log(b); // 7

Destructuring object

let obj = { 
  a: 55,
  b: 44
};

let { a, b } = obj;

console.log(a); // 55
console.log(b); // 44

object property assignment

const a = 2
const b = 5

const obj = { a, b }

// Before es6:
// obj = { a: a, b: b }

console.log(obj) // prints { a: 2, b: 5 }

object function assignment

const obj = { 
    a: 5, 
    b() {
        console.log('b')
    } 
}

obj.b() // prints "b"

spread operator

const a = [ 1, 2 ]
const b = [ 3, 4 ]

const c = [ ...a, ...b ]

console.log(c) // [1, 2, 3, 4]

Object.assign()

const obj1 = { a: 1 }
const obj2 = { b: 2 }

const obj3 = Object.assign({}, obj1, obj2)

console.log(obj3) // { a: 1, b: 2 }

Import/Export Modules

//  lib/intro.js
export function introduction(x) {
    return `Hello ${x}`
}
export const name = 'Leon'

//  app.js
import * as intro from "lib/intro"
console.log(intro.introduction(intro.name))

//  otherApp.js
import { introduction, name } from "lib/intro"
console.log(introduction(name))
Author: Stephen Flannery Jr.