DEV Community

Cover image for 5 JavaScript Tricks I Wish I Knew Earlier. (That You’ll Love Too)
Mohd Shafi
Mohd Shafi

Posted on

5 JavaScript Tricks I Wish I Knew Earlier. (That You’ll Love Too)

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";
Enter fullscreen mode Exit fullscreen mode

2. Destructuring for Cleaner Code
Grab values from objects/arrays like a pro:

const { name, age } = person;
const [first, second] = colors;

Enter fullscreen mode Exit fullscreen mode

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);

Enter fullscreen mode Exit fullscreen mode

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); }

Enter fullscreen mode Exit fullscreen mode

5. Short-Circuit Evaluation
Use && and || to simplify conditional logic:

isLoggedIn && showDashboard();  
const user = input || "Anonymous";

Enter fullscreen mode Exit fullscreen mode

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)