Top 10 JavaScript Tips Every Developer Should Know
JavaScript is the backbone of modern web development, but even experienced developers can overlook subtle features that make coding faster, cleaner, and more efficient. In this post, I’m sharing 10 practical JavaScript tips that can help you level up your skills.
1. Use const
and let
Instead of var
var
has function scope, which can lead to unexpected behavior. Prefer let
for variables that change and const
for constants.
const name = "Prasoon"; // cannot reassign
let age = 17; // can reassign
This reduces bugs and makes your code more predictable.
2. Default Parameters
You don’t need to check for undefined
manually anymore. Use default parameters.
function greet(name = "Guest") {
console.log(`Hello, ${name}!`);
}
greet(); // Hello, Guest!
3. Template Literals
String concatenation with +
is outdated. Template literals are cleaner and allow multi-line strings.
const user = "Alice";
console.log(`Hello, ${user}! Welcome to JavaScript.`);
4. Destructuring Objects and Arrays
Destructuring helps you extract values easily.
const user = { name: "Bob", age: 20 };
const { name, age } = user;
console.log(name, age); // Bob 20
const colors = ["red", "green"];
const [first, second] = colors;
console.log(first, second); // red green
5. Arrow Functions
Arrow functions are concise and do not bind their own this
, making them perfect for callbacks.
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5
6. Short-circuiting with &&
and ||
Instead of if statements, you can use logical operators for simpler conditions.
const user = null;
console.log(user && user.name); // null
console.log(user || "Guest"); // Guest
7. Spread Operator
The spread operator (...
) helps in copying arrays/objects and merging easily.
const arr1 = [1, 2];
const arr2 = [3, 4];
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 4]
8. Optional Chaining
Access nested object properties safely without checking each level.
const user = { profile: { name: "Alice" } };
console.log(user.profile?.name); // Alice
console.log(user.contact?.email); // undefined
9. for…of
for Arrays
Use for…of
instead of for
loops for cleaner iteration.
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(color);
}
10. Debounce Expensive Functions
Avoid performance hits by debouncing functions like scroll or input handlers.
function debounce(fn, delay) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), delay);
};
}
const handleScroll = debounce(() => console.log("Scrolled!"), 300);
window.addEventListener("scroll", handleScroll);
Bonus Tip:
Always keep your code readable. JavaScript is powerful, but messy code is hard to maintain. Tools like ESLint and Prettier can help you enforce best practices.
JavaScript has tons of tricks and best practices. Mastering them not only makes you faster but also writes cleaner, safer, and more efficient code.
Which of these tips do you already use? Or do you have your own favorite JavaScript trick? Share in the comments!
Top comments (0)