JavaScript is full of fun. Do you believe it? You must say WOW while reading our 10 awesome JS tricks. Its guaranteed from our side. Let’s try!
Some JavaScript tricks make coding easier, faster, and more fun. They can also help you write clean and simple code. If you love smart shortcuts, youโll enjoy these JavaScript magic tricks! ๐งโโ๏ธโจ
๐ฉ 1. Swap Two Variables Without a Temp Variable

Usually, we use a temporary variable to swap two values. But thereโs a cool trick to do it in one step. Check it out bro !
let a = 5, b = 10;
[a, b] = [b, a];
console.log(a, b); // 10, 5
๐ฅ Why use this JS Trick?
- Saves spaceโno extra variable needed.
- Looks clean and modern.
๐ก Tip:
This method works best with modern JavaScript (ES6+).
๐ฏ 2. Convert a String to a Number Instantly
Want to turn a string into a number fast? Use the +
sign!
let str = "42";
let num = +str;
console.log(num, typeof num); // 42, 'number'
โจ Why is this useful?
- Shorter than
parseInt()
orNumber()
. - Works instantly.
๐ก Tip:
Be careful! This trick doesnโt work well if the string isnโt a valid number.
๐ต๏ธโโ๏ธ 3. Check If an Object Is Empty in One Line

Instead of checking an objectโs keys manually, use this quick trick.
const isEmpty = obj => Object.keys(obj).length === 0;
console.log(isEmpty({})); // true
console.log(isEmpty({ name: "Alice" })); // false
๐ก Why use this?
- Simple and direct.
- Great for checking API responses.
๐ 4. Get the Last Element of an Array Easily
No need for .length - 1
. Use .at(-1)
instead.
let colors = ["red", "blue", "green"];
console.log(colors.at(-1)); // 'green'
โจ Why is this better?
- Easier to read.
- Supports negative indices.
๐ก Tip:
.at(-1)
works great for both arrays and strings!
๐ฅ 5. Flatten a Deeply Nested Array Quickly
Working with nested arrays? Flatten them with .flat(Infinity)
.
let nestedArr = [1, [2, [3, [4]]]];
let flatArr = nestedArr.flat(Infinity);
console.log(flatArr); // [1, 2, 3, 4]
๐ Why use this?
- No need for loops.
- Super clean and easy.
๐ก Trick:
If you only need to flatten one level, use .flat(1)
instead.
๐ญ 6. Remove Duplicates from an Array Fast

Use Set
to quickly remove duplicate values.
let nums = [1, 2, 2, 3, 4, 4, 5];
let uniqueNums = [...new Set(nums)];
console.log(uniqueNums); // [1, 2, 3, 4, 5]
๐ก Why is this great?
- No need for loops.
- Fast and efficient.
๐ฏ 7. Shorten if-else
Statements with ||
and &&
You can simplify if-else
checks using these operators.
let name = "";
let user = name || "Guest";
console.log(user); // 'Guest'
๐ฅ Why use this?
- Makes the code shorter and easier to read.
- Great for default values.
๐ก Trick:
Use &&
to run a function only if a condition is true.
isLoggedIn && showDashboard();
๐ฒ 8. Shuffle an Array Like a Magician
Want to randomize an array? Try this trick.

let items = [1, 2, 3, 4, 5];
items.sort(() => Math.random() - 0.5);
console.log(items);
๐ฉ Why is this cool?
- One-liner trick.
- Works great for games and quizzes.
๐ก Tip:
This method isnโt perfectly random. For better results, use Fisher-Yates shuffle.
๐ฒ 9. Get a Random Number in a Range Easily
No need for complicated formulas. Use this simple function:

const getRandom = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
console.log(getRandom(1, 10));
๐ฏ Why is this useful?
- Perfect for random features in apps.
- Super easy to remember.
๐ก Tip:
Always set a range to avoid unexpected results.
โณ 10. Measure Code Execution Time
Use console.time()
to check how long a function takes to run.
console.time("test");
for(let i = 0; i < 1000000; i++) {}
console.timeEnd("test");

๐ Why is this handy?
- Helps debug slow code.
- Great for performance optimization.
๐ก Tip:
Use performance.now()
for more accurate timing in large applications.
๐ Conclusion: Master JavaScript Like a Pro!
These JavaScript tricks help you write better code, save time, and impress others. Use them in your projects and see the difference!
๐ก Which trick did you like the most? Let us know! ๐
๐๏ธโโ๏ธ Discover Code Blocks From 20+ yrs JS Expert
๐ฅ Asp.net C# Developer
๐ Solution Architect
๐จโโ๏ธ Database Administrator
๐ข Speaker
๐ MCTS since 2009
Leave a Reply