JavaScript is one of the most flexible languages in the world.
But most developers only use 30–40% of what the language actually offers.
After reviewing hundreds of GitHub projects and working on multiple production apps, I noticed something interesting:
Great JavaScript developers often rely on small language features that dramatically improve readability and reduce bugs.
Here are 7 JavaScript tricks that instantly make your code cleaner and more professional.
1. Destructuring Instead of Repeating Object Names
Most beginners write code like this:
const user = getUser();
const name = user.name;
const email = user.email;
const age = user.age;
Cleaner approach:
const { name, email, age } = getUser();
Benefits:
- Less repetition
- Easier to read
- Cleaner refactoring
2. Default Parameters (Stop Writing If Statements)
Instead of this:
function greet(name) {
if (!name) {
name = "Guest";
}
return `Hello ${name}`;
}
Use this:
function greet(name = "Guest") {
return `Hello ${name}`;
}
Much simpler.
3. Optional Chaining Prevents Crashes
Ever seen this error?
Cannot read property 'name' of undefined
Instead of writing multiple checks:
if (user && user.profile && user.profile.name) {
console.log(user.profile.name);
}
Use optional chaining:
console.log(user?.profile?.name);
Cleaner and safer.
4. Object Property Shorthand
A surprising number of developers still write this:
const name = "Alex";
const age = 25;
const user = {
name: name,
age: age
};
Modern JavaScript allows this:
const user = { name, age };
Less code. Same result.
5. Array .at() for Cleaner Indexing
Instead of:
const lastItem = array[array.length - 1];
Use:
const lastItem = array.at(-1);
It's easier to read and avoids mistakes.
6. Nullish Coalescing (??) Is Better Than ||
Many developers use:
const username = input || "Guest";
But this breaks if input = "" or 0.
Better solution:
const username = input ?? "Guest";
?? only replaces null or undefined, not valid values.
7. Use Object.fromEntries() to Transform Objects
Example:
const entries = [
["name", "Sam"],
["age", 28]
];
const obj = Object.fromEntries(entries);
Result:
{ name: "Sam", age: 28 }
This is extremely useful when transforming data.
Final Thought
Clean code isn’t about writing more code.
It’s about writing less code that communicates more.
Small JavaScript features like these can:
- reduce bugs
- improve readability
- make your code feel more professional
💬 Your turn:
What’s one JavaScript trick you use that most developers don’t know about?
Please visit my website
aweb021.blogspot.com
Top comments (1)
My portfolio
aryoncompany.tiiny.site