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.
Key Differences:
let- Block-scoped, can be reassignedconst- Block-scoped, cannot be reassignedvar- Function-scoped, hoisted (avoid)
Best Practices:
- Use
constby default - Use
letwhen reassignment needed - Avoid
varin 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.
⚠️ When NOT to Use Arrow Functions:
- Object methods that need
this - Constructor functions
- Functions using
argumentsobject - 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.
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.
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.
💡 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 variableconst- Block-scoped constantvar- Function-scoped (avoid)
Functions:
() =>- Arrow functions(a = 1) =>- Default parameters(...args) =>- Rest parameters
Strings:
`text $$ { }`- Template literalstag`text`- Tagged templatesstr.startsWith()- New methods
Objects/Arrays:
[a, b]- Shorthand properties...obj- Spread operator[a, ...rest]- Array destructuring[a, ...rest]- Object destructuring