JavaScript is constantly evolving, and mastering it goes beyond the basics. Whether you're a beginner or a seasoned developer, these 10 JavaScript tricks will boost your productivity, improve your code readability, and help you write cleaner, smarter code.
1. Destructuring Assignment
Simplify object and array access:
Example:
const user = { name: 'Ali', age: 25 };
const { name, age } = user;
2. Spread & Rest Operators
Combine or clone arrays/objects easily:
Example:
const arr = [1, 2, 3];
const newArr = [...arr, 4]; // [1, 2, 3, 4]
function sum(...nums) {
return nums.reduce((a, b) => a + b);
}
3. Optional Chaining (?.) & Nullish Coalescing (??)
Safe property access and default values:
Example:
const user = {};
console.log(user.profile?.name ?? 'Guest');
4. Short-Circuit Evaluation for Defaults
Cleaner than if statements:
Example:
let isLoggedIn = user.isLoggedIn || false;
5. Convert Anything to Boolean with !!
Example:
!!"" // false
!!"JS" // true
6. Swap Variables Without Temp
Example:
let a = 1, b = 2;
[a, b] = [b, a];
7. Array Methods Shortcuts
Example:
[1, 2, 3].includes(2); // true
[1, 2, 3].at(-1); // 3 (last item)
8. Object.entries + map = Magic
Loop over objects like arrays:
Example:
const obj = { a: 1, b: 2 };
Object.entries(obj).map(([key, val]) => console.log(key, val));
9. Remove Duplicates from Array
Example:
const nums = [1, 2, 2, 3];
const unique = [...new Set(nums)];
10. Dynamic Property Names
Example:
const key = 'score';
const obj = { [key]: 100 }; // { score: 100 }
⚡ Final Thoughts
These tricks might seem small, but when used consistently, they make your JavaScript cleaner, faster, and easier to debug. Master these, and you’ll impress other devs with how efficient your code looks!

0 Comments