ES6+ Modern JavaScript Features

📦 let, const, and Block Scoping

ES6 introduced let and const for better variable scoping and immutability. These replace the problematic var keyword.

JavaScript Editor
Key Differences:
  • let - Block-scoped, can be reassigned
  • const - Block-scoped, cannot be reassigned
  • var - Function-scoped, hoisted (avoid)
Best Practices:
  • Use const by default
  • Use let when reassignment needed
  • Avoid var in new code
  • Declare variables at the top of their scope

🏹 Arrow Functions

Arrow functions provide a concise syntax and lexical this binding. They're perfect for callbacks and functional programming patterns.

JavaScript Editor
⚠️ When NOT to Use Arrow Functions:
  • Object methods that need this
  • Constructor functions
  • Functions using arguments object
  • Event handlers that need dynamic this

💬 Template Literals

Template literals make string interpolation and multi-line strings much cleaner. Tagged templates enable powerful DSLs and string processing.

JavaScript Editor
Template Literal Features:
  • String interpolation with $$ { }
  • Multi-line strings without escaping
  • Tagged templates for custom processing
  • Expression evaluation
Use Cases:
  • Dynamic HTML generation
  • SQL/query building (carefully!)
  • Internationalization
  • CSS-in-JS solutions

🎯 Destructuring

Destructuring allows you to extract values from arrays and objects into distinct variables with a clean, declarative syntax.

JavaScript Editor
Array Destructuring:
  • Extract values by position
  • Skip elements with commas
  • Use default values
  • Collect rest with ...
Object Destructuring:
  • Extract by property name
  • Rename variables
  • Nested destructuring
  • Default values
Common Patterns:
  • Function parameters
  • Import statements
  • API response handling
  • Configuration objects

🌀 Spread & Rest Operators

The spread (...) operator expands iterables, while the rest (...) operator collects multiple elements. They're incredibly versatile for modern JavaScript.

JavaScript Editor
💡 Memory Consideration:

Spread operators create shallow copies. For deep copies of nested objects, use JSON.parse(JSON.stringify(obj)) or libraries like Lodash'scloneDeep. Also, spreading large arrays/objects can be memory intensive.

📋 ES6+ Quick Reference

Variables:
  • let - Block-scoped variable
  • const - Block-scoped constant
  • var - Function-scoped (avoid)
Functions:
  • () => - Arrow functions
  • (a = 1) => - Default parameters
  • (...args) => - Rest parameters
Strings:
  • `text $$ { }` - Template literals
  • tag`text` - Tagged templates
  • str.startsWith() - New methods
Objects/Arrays:
  • [a, b] - Shorthand properties
  • ...obj - Spread operator
  • [a, ...rest] - Array destructuring
  • [a, ...rest] - Object destructuring