ES6 Modern JavaScript

Developer Directory.

arrow functions destructuring template literals spread optional chaining nullish coalescing
/>
Showing 0
Available 0
ES6 Patterns Used In This File
Arrow Functions
// old
function add(a, b) { return a + b }

// ES6
const add = (a, b) => a + b

// No return needed for one-liners
Destructuring
const dev = { name: 'Wardell', role: 'FE' }

// Pull values out cleanly
const { name, role } = dev

// Same with arrays
const [first, second] = ['a', 'b']
Template Literals
const name = 'Wardell'

// Backtick strings with ${}
const msg = `Hello, ${name}`

// Can span multiple lines too
Spread Operator
const a = [1, 2, 3]
const b = [...a, 4, 5]
// [1, 2, 3, 4, 5]

// Copy + update an object
const s2 = { ...state, query: 'React' }
Optional Chaining
const dev = { skills: ['React'] }

// Won't crash if undefined
const first = dev?.skills?.[0]

// Returns undefined, not an error
Nullish Coalescing
// Use fallback only if null/undefined
const rate = dev.rate ?? 'N/A'

// Different from ||
// 0 ?? 'N/A'  →  0  (keeps 0)
// 0 || 'N/A'  →  'N/A' (0 is falsy)