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

9 Hidden JavaScript Features You Probably Donโ€™t Know Exist ๐Ÿคฏ

Shawpnendu Bikash Maloroy

March 3, 2025
9 Hidden JavaScript Features
Spread the love

JavaScript has many hidden tricks that most developers donโ€™t know yet. These 9 hidden JavaScript features can make coding easier and save your time a lot. Letโ€™s explore all 9 amazing JavaScript secrets with us! ๐Ÿš€ Welcome to our blog and enhance your JS coding skills with our tutorials!

Table of Contents

  • ๐Ÿง 1. The ?? Nullish Coalescing Operator
  • ๐Ÿ”ฅ 2. The ?. Optional Chaining Operator
  • ๐ŸŽญ 3. Object.hasOwn() โ€“ A Safer hasOwnProperty()
  • โšก 4. replaceAll() for String Replacements
  • ๐Ÿ† 5. The at() Method for Arrays and Strings
  • ๐ŸŽฉ 6. Numeric Separators for Readable Numbers
  • ๐Ÿ› ๏ธ 7. structuredClone()Deep Copy Made Easy
  • ๐Ÿง™ 8. groupBy() for Arrays (Coming Soon!)
  • ๐Ÿ’ก Bonus: Math.trunc() for Quick Integer Extraction
  • โ“ Frequently Asked Questions (FAQ)
    • 1. What is the best hidden feature in JavaScript?
    • 2. How do I deep copy an object in JavaScript?
    • 3. Why is at() better than using array.length – 1?
    • 4. What does replaceAll() do in JavaScript?
    • 5. Can I use groupBy() in JavaScript now?
    • 6. What is the difference between || and ?? in JavaScript?
    • 7. How can I make large numbers more readable in JavaScript?
  • Conclusion

๐Ÿง 1. The ?? Nullish Coalescing Operator

?? helps when checking for null or undefined. It is better than || because it does not treat "" or 0 as false. Unlike ||, it only considers null and undefined as falsy values, making it safer for real data.

JavaScript Double Question Mark vs Or: A Comprehensive Guide

let username = "";
console.log(username || "Guest");  // Outputs: Guest โŒ
console.log(username ?? "Guest");  // Outputs: "" โœ…
OperatorWorks OnProblemSolution
||Any falsy valueTreats "" and 0 as falseNot ideal for all cases
??Only null or undefinedSafer for real valuesโœ… Better choice

๐Ÿ”ฅ 2. The ?. Optional Chaining Operator

No more TypeErrors when dealing with nested objects! ๐Ÿš€

let user = { profile: { name: "Alice" } };
console.log(user.profile?.name);   // Outputs: Alice โœ…
console.log(user.settings?.theme); // Outputs: undefined โœ…

๐ŸŽญ 3. Object.hasOwn() โ€“ A Safer hasOwnProperty()

Object.hasOwn() is better and safer for checking if an object really owns a property.

const obj = { key: "value" };
console.log(Object.hasOwn(obj, "key")); // true โœ…
console.log(Object.hasOwn(obj, "toString")); // false โœ…

โšก 4. replaceAll() for String Replacements

Use replaceAll() to replace multiple matches easily.

let text = "banana apple banana orange";
console.log(text.replaceAll("banana", "grape"));
// Outputs: "grape apple grape orange"
JS replaceAll() for String Replacements

๐Ÿ† 5. The at() Method for Arrays and Strings

at() makes it easy to get the last item in an array. ๐Ÿ”ฅ

const fruits = ["apple", "banana", "cherry"];
console.log(fruits.at(-1)); // Outputs: "cherry" โœ…

๐ŸŽฉ 6. Numeric Separators for Readable Numbers

Big numbers can be hard to read. _ makes them clearer. ๐Ÿคฏ

const billion = 1_000_000_000;
console.log(billion); // Outputs: 1000000000 โœ…

๐Ÿ› ๏ธ 7. structuredClone()Deep Copy Made Easy

JS Deep Copy

Forget manual deep cloningโ€”JavaScript now has a built-in way! ๐ŸŽ‰

const obj = { name: "Alice", skills: ["JS", "React"] };
const clone = structuredClone(obj);
clone.skills.push("Node.js");
console.log(obj.skills); // Outputs: ["JS", "React"] โœ…

๐Ÿง™ 8. groupBy() for Arrays (Coming Soon!)

Grouping array items will soon be super easy. ๐Ÿคฉ

JS groupBy() for Arrays
const items = ["apple", "banana", "grape", "blueberry"];
console.log(groupBy(items, (item) => item.length));
// Outputs: { 5: ["apple", "grape"], 6: ["banana"], 9: ["blueberry"] }

๐Ÿ’ก Bonus: Math.trunc() for Quick Integer Extraction

Math.trunc() removes decimals without rounding up or down! ๐Ÿ”ฅ

console.log(Math.trunc(4.9)); // Outputs: 4 โœ…
console.log(Math.trunc(-4.9)); // Outputs: -4 โœ…
MethodInputOutputRemarks
Math.trunc()4.94Removes decimals without rounding
Math.trunc()-4.9-4Same behavior for negative numbers
Math.floor()4.94Rounds down to the nearest integer
Math.ceil()4.15Rounds up to the nearest integer
Math.round()4.55Rounds to the nearest integer

โ“ Frequently Asked Questions (FAQ)

1. What is the best hidden feature in JavaScript?

The ?? (Nullish Coalescing) operator is one of the best because it makes handling null and undefined super easy.

2. How do I deep copy an object in JavaScript?

Use structuredClone() instead of JSON.parse(JSON.stringify()) for a more reliable deep copy.

3. Why is at() better than using array.length - 1?

at(-1) is cleaner and easier to read compared to array[array.length - 1].

4. What does replaceAll() do in JavaScript?

It replaces all occurrences of a string instead of just the first one, unlike replace().

5. Can I use groupBy() in JavaScript now?

Not yet! Itโ€™s coming in future versions of JavaScript.

6. What is the difference between || and ?? in JavaScript?

|| treats all falsy values (like 0, "", null, undefined) as false, while ?? only checks for null or undefined.

7. How can I make large numbers more readable in JavaScript?

Use numeric separators like _ (e.g., 1_000_000) to improve readability without changing the number value.

Conclusion

JavaScript has many hidden tricks that can save time and make coding fun. Try them out and let us know which one is your favorite! ๐Ÿ˜ƒโœจ

๐Ÿš€ Did you enjoy this? Share it with other JavaScript lovers! ๐Ÿ’™

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