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
??
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: "" 
Operator | Works On | Problem | Solution |
---|---|---|---|
|| | Any falsy value | Treats "" and 0 as false | Not ideal for all cases |
?? | Only null or undefined | Safer for real values |
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"

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

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.

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 
Method | Input | Output | Remarks |
---|---|---|---|
Math.trunc() | 4.9 | 4 | Removes decimals without rounding |
Math.trunc() | -4.9 | -4 | Same behavior for negative numbers |
Math.floor() | 4.9 | 4 | Rounds down to the nearest integer |
Math.ceil() | 4.1 | 5 | Rounds up to the nearest integer |
Math.round() | 4.5 | 5 | Rounds 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!
Discover Code Blocks From 20+ yrs JS Expert
Asp.net C# Developer
Solution Architect
Database Administrator
Speaker
MCTS since 2009
Leave a Reply