Learn the recommended coding standards and techniques that separate clean, maintainable code from code that creates future headaches.
JavaScript Best Practices are recommended coding standards and techniques used to write clean, efficient, maintainable, and error-free code.
They are not enforced by the language itself — but following them makes your code easier to read, debug, and scale over time.
Best practices mean: "Write code in a way that future you (and others) won't hate you."
Single-letter names tell you nothing about what the code does.
let a = 10;
let b = 20;
let c = a + b;
console.log(c);
let firstNumber = 10;
let secondNumber = 20;
let totalSum = firstNumber + secondNumber;
console.log(totalSum);
count lives globally — any part of the program can accidentally change it.
var count = 0;
function increment() {
count++;
}
increment();
console.log(count);
function increment() {
let count = 0;
count++;
console.log(count);
}
increment();
let / const inside a function) for safetyThe string "Hello" is re-evaluated as a literal on every iteration.
for (let i = 0; i < 5; i++) {
console.log("Hello");
}
let message = "Hello";
for (let i = 0; i < 5; i++) {
console.log(message);
}
Imagine writing notes for exams:
Best practices shine in real HTML pages too. Notice the function, clear naming, and input validation below. Click ▶ Preview to run it live.
<!DOCTYPE html>
<html>
<head><title>Best Practices</title></head>
<body style="font-family:sans-serif;padding:20px;background:#f8f9fa">
<h2>User Greeting</h2>
<p id="output"></p>
<script>
function greetUser(userName) {
if (!userName) {
return "Hello, Guest";
}
return "Hello, " + userName;
}
let name = "Aman";
document.getElementById("output").innerText = greetUser(name);
</script>
</body>
</html>
greetUser, userName)let x = 5;
let y = 10;
let z = x * y;
console.log(z);
var total = 0;
function add() {
total += 10;
}
total variableadd() self-contained and return the resultconsole.log()totalPrice make the purpose immediately obvious — no guessing required.
Use let and const instead of var. var has function scope and hoisting quirks that cause subtle bugs.
const MAX_SCORE = 100; // value never changes
let currentScore = 0; // value will change
console.log(MAX_SCORE, currentScore);
Follow camelCase naming conventions for variables and functions:
// camelCase — variables and functions
let userName = "Aman";
function calculateTotal() {}
// UPPER_SNAKE_CASE — constants
const MAX_RETRIES = 3;
Keep functions small and focused — each function should do one thing only. This makes testing and debugging dramatically easier.
Follow the DRY principle — Don't Repeat Yourself. If you copy-paste code more than once, put it in a function.
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Aman");
greet("Sara");
greet("Ali");
Spot the best-practice violation — then think about how you'd improve it before revealing the answer.
let a = 10;
function test() {
a = 20;
}
test();
console.log(a);
// Problem:
// 'a' is a global variable — test() silently mutates it.
// This is hard to track in larger codebases.
// Output of original: 20 (a was changed globally)
// Improved version — no global mutation:
function test() {
let a = 20; // local to this function
console.log(a); // prints 20
}
test();
// The outer 'a' is untouched — safe and predictable.