CodersTechZone
  • .NET
    • C#
    • ASP.Net
  • HTML
  • Javascript
  • CSS
  • Database
    • SQL Server
    • MYSql
    • Oracle
  • AI
  • TechNews
  • Web-Stories

10 JavaScript Tricks That Will Blow Your Mind! ๐Ÿš€โœจ

Shawpnendu Bikash Maloroy

March 12, 2025
10 JavaScript Tricks - That Will Blow Your Mind!
Spread the love

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

JS - 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() or Number().
  • 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

Check If an Object Is Empty

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

JS - Remove Duplicates

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.

Shuffle an Array Like a Magician Using JS
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:

javaScript Tricks - Get a Random Number in a Range
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");
Measure JS Code Execution Time

๐Ÿš€ 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! ๐Ÿš€

Shawpnendu Bikash Maloroy
Shawpnendu Bikash Maloroy

๐Ÿ‹๏ธโ€โ™‚๏ธ Discover Code Blocks From 20+ yrs JS Expert
๐Ÿ’ฅ Asp.net C# Developer
๐Ÿ† Solution Architect
๐Ÿ‘จโ€โœˆ๏ธ Database Administrator
๐Ÿ“ข Speaker
๐ŸŽ“ MCTS since 2009

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Spread the love
ยซPrevious
Nextยป

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • 7 Weird JavaScript Tricks That Look Like Bugs
    7 Weird JavaScript Tricks That Look Like Bugs (#3 Will Shock You!)
  • Build a JavaScript Gamer Journal in 8 Lines
    ๐ŸŽฎ Build a JavaScript Gamer Journal in 8 Lines: Track Your Wins Like a Pro! ๐Ÿ†
  • JavaScript Pet Feeder Reminder in 7 Lines
    How to Code a Simple JavaScript Pet Feeder Reminder in 7 Lines: Feed Your Pet Like a Coding Boss! ๐Ÿถ
  • 10-line calculator JavaScript
    Build a Simple Calculator in JavaScript: 10 Lines to Wow Your Friends!
  • JavaScript No-Code Automation
    JavaScript No-Code Automation: How to Replace Zapier with a Few Lines of Code

About-CodersTech Zone |  Contact |  Disclaimer |  Fact-Checking policy |  Affiliate Disclosure |  Privacy Policy

Copyright ยฉ 2024 CodersTechZone.com