Welcome to this guide on Modern JavaScript Best Practices. JavaScript has evolved massively since the introduction of ES6 (ECMAScript 2015). If you are still writing JS the way it was taught in 2010, your code is likely harder to read, more prone to bugs, and difficult to maintain.

In this article, we will compare legacy patterns with their modern, elegant solutions.

1. Say Goodbye to var

Historically, JavaScript only had var for declaring variables. This caused massive headaches because var is function-scoped, meaning variables could โ€œleakโ€ out of for loops and if blocks.

The Best Practice: Completely stop using var.

  • Use const by default for everything.
  • If you know a variableโ€™s value needs to change later (like a counter in a loop), use let. Both of these are block-scoped.
// โŒ BAD
var userName = "Admin";

// โœ… GOOD
const userName = "Admin";
let loginAttempts = 0;

2. Embrace Template Literals

Combining strings and variables used to involve a messy soup of plus signs and quotes.

The Best Practice: Use backticks ` to create Template Literals. You can inject variables directly using ${} syntax. As a bonus, template literals support multi-line strings automatically without needing \n.

// โŒ BAD
const msg = "Welcome " + name + ", you have " + messages + " messages.";

// โœ… GOOD
const msg = `Welcome ${name}, you have ${messages} messages.`;

3. Destructuring Objects and Arrays

When working with APIs, you often receive large JSON objects and need to extract data from them. Repeating object.property over and over violates the DRY (Donโ€™t Repeat Yourself) principle.

The Best Practice: Use Object Destructuring to unpack properties from objects into distinct variables in a single line.

const user = { id: 42, name: 'Alice', role: 'admin' };

// โŒ BAD
const id = user.id;
const name = user.name;
const role = user.role;

// โœ… GOOD
const { id, name, role } = user;

4. Arrow Functions

Arrow functions offer a shorter syntax and, more importantly, they donโ€™t bind their own this keyword, making them perfect for callbacks.

// โŒ BAD
const items = [1, 2, 3];
const doubled = items.map(function(item) {
  return item * 2;
});

// โœ… GOOD
const doubled = items.map(item => item * 2);

By adopting these modern ES6+ features, your codebase will become cleaner, more predictable, and much easier for other developers to read. Check out the Live Demo for an interactive cheat sheet comparing these code styles side-by-side!