The "Before and After" Transformation
The Dark Ages (Pre-ES6)
javascript
// The old way - verbose and error-prone
var PI = 3.14159;
var radius = 5;
var calculateArea = function(r) {
return PI * r * r;
};
var area = calculateArea(radius);
console.log('Area is: ' + area);
// Callback hell
getUser(function(user) {
getOrders(user.id, function(orders) {
getProducts(orders[0].id, function(products) {
renderPage(user, orders, products);
});
});
});
The Renaissance (ES6+)
javascript
// The new way - clean and expressive
const PI = 3.14159;
const radius = 5;
const calculateArea = r => PI * r * r;
const area = calculateArea(radius);
console.log(`Area is: ${area}`);
// Async elegance
const user = await getUser();
const orders = await getOrders(user.id);
const products = await getProducts(orders[0].id);
renderPage(user, orders, products);
Top 10 ES6+ Features That Changed Everything
1. Arrow Functions: Goodbye function Keyword
javascript
// Before
arr.map(function(item) {
return item * 2;
});
// After - One-liner elegance
arr.map(item => item * 2);
// Multiple parameters
arr.reduce((acc, curr) => acc + curr, 0);
// Multiple lines
arr.filter(item => {
const isValid = validate(item);
return isValid && item.active;
});
Pro Tip: Arrow functions don't have their own this—they inherit it from the parent scope. Perfect for callbacks!
2. Template Literals: The End of String Concatenation
javascript