As a front-end dev still sharpening my skills, JavaScript keeps surprising me. Here are 5 simple but powerful JS tricks I wish I had discovered earlier — and now use all the time:
1. Optional Chaining (?.
)
No more TypeError: cannot read property of undefined!
const userName = user?.profile?.name ?? "Guest";
2. Destructuring for Cleaner Code
Grab values from objects/arrays like a pro:
const { name, age } = person;
const [first, second] = colors;
3. Array.some()
and Array.every()
Check if at least one or all elements meet a condition:
items.some(item => item.stock > 0);
items.every(item => item.stock > 0);
4. Spread & Rest Operators
Clone, merge, or handle multiple arguments:
const newArray = [...arr1, ...arr2];
function sum(...nums) { return nums.reduce((a, b) => a + b, 0); }
5. Short-Circuit Evaluation
Use &&
and ||
to simplify conditional logic:
isLoggedIn && showDashboard();
const user = input || "Anonymous";
These tricks aren't advanced — just practical. Once I started using them, my code became much cleaner and easier to understand. If you're learning JavaScript, give these a try!
What are your favorite JS tips? Drop them below!
Top comments (0)