An interactive comparison between legacy code and modern ES6+ clean code.
1. Variable Declaration
❌ Legacy (Avoid)
// 'var' causes hoisting and scope issuesvar name = "John";
var name = "Doe"; // No error thrown!for (var i = 0; i < 5; i++) {
// i leaks out of this block
}
✅ Modern (Use const/let)
// Default to 'const', use 'let' if it changesconst name = "John";
// const name = "Doe"; // Error! Safe.for (let i = 0; i < 5; i++) {
// i is securely block-scoped
}
2. String Concatenation
❌ Legacy (Avoid)
const user = "Alice";
const age = 25;
// Messy quotes and plus signsconst message = "Hello, " + user + "!\n" +
"You are " + age + " years old.";
✅ Modern (Template Literals)
const user = "Alice";
const age = 25;
// Clean, readable, supports multi-lineconst message = `Hello, ${user}!
You are ${age} years old.`;
3. Object Destructuring
❌ Legacy (Avoid)
const person = {
first: "Bob",
last: "Smith",
city: "NY"
};
// Repetitive property accessconst first = person.first;
const last = person.last;
const city = person.city;
✅ Modern (Destructuring)
const person = {
first: "Bob",
last: "Smith",
city: "NY"
};
// Extract variables in one single lineconst { first, last, city } = person;