1️⃣ Use strict mode when appropriate
Strict mode helps catch certain common mistakes and prevents some unsafe behaviors.
"use strict";
2️⃣ Use optional chaining for uncertain data
When working with API responses or nested objects, optional chaining can prevent errors when a property doesn't exist.
const user = {};
console.log(user.address?.city);3️⃣ Understand || vs ??
They behave differently when dealing with falsy values.
const count = 0;
console.log(count || 10); // 10
console.log(count ?? 10); // 0
Use
?? when 0, false, or an empty string are valid values.4️⃣ Don't overuse ternary operators
Ternary operators are great for simple conditions.
const age = 20;
const status = age >= 18 ? "Adult" : "Minor";
For complicated logic, a normal
if...else is usually easier to read.5️⃣ Use destructuring when it improves readability
Destructuring can make working with objects and arrays cleaner.
const user = {
name: "Alex",
age: 25
};
const { name, age } = user;6️⃣ Understand spread vs rest
The same
... syntax has two different purposes.Spread → expands values
const numbers = [1, 2, 3];
const copy = [...numbers];
Rest → collects values
function sum(...numbers) {
return numbers.length;
}7️⃣ Don't rely on implicit type coercion
JavaScript can automatically convert types, sometimes producing surprising results.
console.log("10" + 5); // "105"
console.log("10" - 5); // 5Be explicit when conversion matters.
8️⃣ Handle asynchronous errors properly
Don't assume every API request or asynchronous operation will succeed.
async function getData() {
try {
const response = await fetch("/api/data");
const data = await response.json();
return data;
} catch (error) {
console.error("Request failed:", error);
}
}9️⃣ Don't block the main thread
Heavy calculations or large amounts of synchronous work can make a web application unresponsive.
For expensive tasks, consider:
• Breaking work into smaller tasks
• Web Workers
• Efficient algorithms
• Avoiding unnecessary rendering
🔟 Use debounce and throttle appropriately
These techniques are useful when an event fires repeatedly.
Debounce → Run after activity stops.
Throttle → Limit execution to a controlled frequency.
Common use cases: Search inputs, Scrolling, Resizing, Mouse movement
1️⃣1️⃣ Use const objects carefully
const prevents reassignment of the variable, but it doesn't make the object immutable.const user = {
name: "Alex"
};
user.name = "Sam"; // AllowedUnderstanding this distinction is important.
1️⃣2️⃣ Don't blindly use JSON.parse() and JSON.stringify()
They are useful for JSON data, but they aren't universal deep-copy solutions. They can lose or alter certain JavaScript values and don't preserve all object types.
For structured cloning, consider
structuredClone() when appropriate.1️⃣3️⃣ Use === consistently
Strict equality makes your comparisons easier to reason about.
if (status === "active") {
console.log("User is active");
}