Most developers use JavaScript every day.
But few know the small tricks that make a big difference.
Here are 10 simple but powerful JavaScript tips that can help you write cleaner, smarter code.
1. Optional Chaining (?.
)
Access deeply nested values without breaking your code.
const user = {
name: "Alex",
profile: {
age: 30
}
};
console.log(user.profile?.age); // 30
console.log(user.settings?.theme); // undefined (no error)
Why it matters
You don’t need to keep writing if
statements or &&
chains. It just works.
2. Nullish Coalescing (??
)
A better way to set fallback values.
const input = "";
const value = input ?? "Default";
console.log(value); // "" (not "Default")
Why it's better
It only falls back if the value is null
or undefined
, not falsy like ""
or 0
.
3. Short-Circuit Evaluation
Run code only if a condition is true.
let isLoggedIn = true;
isLoggedIn && console.log("Welcome back!");
Cleaner than:
if (isLoggedIn) {
console.log("Welcome back!");
}
4. Destructuring Objects and Arrays
Extract values in one line.
const user = { name: "Sarah", age: 25 };
const { name, age } = user;
const numbers = [1, 2, 3];
const [first, second] = numbers;
Why it helps
Less code. More clarity.
5. Default Function Parameters
No more manual checks inside functions.
function greet(name = "Guest") {
console.log(`Hello, ${name}`);
}
greet(); // Hello, Guest
6. Template Literals
Build strings without the messy +
.
const name = "John";
const age = 28;
console.log(`My name is ${name} and I am ${age} years old.`);
7. Spread and Rest Operators
Combine or split data easily.
const arr = [1, 2, 3];
const newArr = [...arr, 4, 5];
const sum = (...nums) => nums.reduce((a, b) => a + b, 0);
console.log(sum(1, 2, 3)); // 6
8. Object Property Shorthand
Cleaner object definitions.
const name = "Liam";
const age = 22;
const user = { name, age };
Instead of:
const user = { name: name, age: age };
9. Array .at()
Method
Access items by position, including negatives.
const letters = ["a", "b", "c", "d"];
console.log(letters.at(-1)); // "d"
Why use it
It’s clearer than arr[arr.length - 1]
.
10. Double Bitwise NOT for Floor
Fast alternative to Math.floor()
.
console.log(~~4.9); // 4
console.log(~~-4.9); // -4
Caution:
Only works well with positive or simple floats. Avoid with big numbers or decimals you care about.
Final Thought
Great code isn’t just about knowing how something works.
It’s about knowing when to use it — and using it wisely.
These tricks won’t make you a 10x dev overnight.
But they’ll help you write code that’s easier to read, easier to debug, and harder to mess up.
And that’s a great place to start.
Top comments (0)