Meet Shawpnendu, a JavaScript developer who writes code daily but misses out on powerful JavaScript features. Just like most developers, he repeats old coding habits without realizing how much easier his life could be! ๐ฑ
Do you want to make the same mistake? If not then learn Shawpnendu’s mistakes and become a rock solid JS developer soon.
So without wasting your time letโs begin hidden JavaScript features that 90% of devs never use – but should! ๐
๐ 1. Optional Chaining (?.
)
Problem:
When accessing deeply nested properties, we often check if an object exists before accessing its properties. This makes our code more messy with full of conditions and lengthy unnecessarily.
Solution:
Use ?.
to safely access properties without errors.
โ Old way:
let user = { profile: { name: "Alice" } };
if (user && user.profile && user.profile.name) {
console.log(user.profile.name);
} else {
console.log("Name not found");
}
// Output: Alice
โ New way:
let user = { profile: { name: "Alice" } };
console.log(user?.profile?.name || "Name not found");
// Output: Alice
Code Explanation:

?.
checks if the previous property exists.- If
user.profile
doesnโt exist, it returnsundefined
instead of an error. - The
|| "Name not found"
ensures a fallback value.
๐ก Tip:
Use ?.
when working with API responses or large objects where properties might not always exist.
โก 2. Nullish Coalescing (??
)
Problem:
Using ||
for fallback values treats 0
, ""
, and false
as falsy, even when they are valid.
Solution:
Use ??
to check only null
or ****undefined
.
โ Old way:
let count = 0;
console.log(count || 10);
// Output: 10 (Incorrect, because 0 is valid!)
โ New way:
let count = 0;
console.log(count ?? 10);
// Output: 0 (Correct!)
Code Explanation:

||
sees0
as falsy, so it replaces it with10
.??
only checks fornull
orundefined
, keeping0
as valid.
๐ Tip:
Use ??
when you want to keep valid values like 0
or ****""
.
๐ Special Note: Nullish coalescing is useful when handling user inputs where values like
0
should be preserved instead of being replaced.
๐ฏ **3. Object.hasOwn()
Instead of **hasOwnProperty()
Problem:
hasOwnProperty()
can be tricked by overridden methods.
Solution:
Use Object.hasOwn()
for a safer check.
โ Risky way:
const obj = Object.create(null);
obj.hasOwnProperty = () => false;
console.log(obj.hasOwnProperty("key"));
// Output: TypeError! โ
โ Safe way:
const obj = { key: "value" };
console.log(Object.hasOwn(obj, "key"));
// Output: true โ

๐ก Tip:
Object.hasOwn()
is more reliable when working with inherited properties or prototype chains.
๐ฅ 4. The at()
Method for Arrays

Problem:
Getting the last item in an array is messy and hard to read.
Solution:
Use .at(-1)
for cleaner code.
โ Old way:
const arr = ["apple", "banana", "cherry"];
console.log(arr[arr.length - 1]);
// Output: "cherry" ๐
โ New way:
const arr = ["apple", "banana", "cherry"];
console.log(arr.at(-1));
// Output: "cherry" โ
๐ Tip:
.at(-1)
works on both arrays and strings, making it more versatile.
๐ ๏ธ 5. structuredClone()
for Deep Copying Objects
Problem:
JSON.parse(JSON.stringify())
fails for complex objects.

Solution:
Use structuredClone()
for a real deep copy.
โ New way:
const obj = { name: "Alice", skills: ["JS", "React"] };
const clone = structuredClone(obj);
clone.skills.push("Node.js");
console.log(obj.skills);
// Output: ["JS", "React"] โ
(Original remains unchanged)
๐ก Tip:
Use structuredClone()
instead of third-party libraries for deep copying objects.
๐ Comparison Table
Feature | Old Method | New Method | Why It’s Better? |
---|---|---|---|
Optional Chaining | Long if conditions | ?. operator | Cleaner & safer code |
Nullish Coalescing | Checks all falsy values | ?? only checks null & undefined | Avoids unwanted overrides |
Object.hasOwn() | hasOwnProperty() | Object.hasOwn() | More reliable security |
Array.at() | arr[arr.length - 1] | arr.at(-1) | Easier to read & use |
structuredClone() | JSON.parse(JSON.stringify()) | structuredClone() | Proper deep copying |
๐ข FAQs
1. What is the most useful hidden JavaScript feature?
Optional chaining (?.
) is one of the best hidden features that makes accessing deeply nested properties safe and clean.
2. How does ??
differ from ||
?
??
only checks for null
or undefined
, while ||
considers all falsy values like 0
, false
, and ""
.
3. Why is Object.hasOwn()
better than hasOwnProperty()
?
Object.hasOwn()
avoids prototype-related issues, making property checks more reliable.
4. Is structuredClone()
better than JSON methods?
Yes, it supports complex data types like Map
and Set
, which JSON.parse(JSON.stringify())
fails to copy.
5. What is the easiest way to get the last item of an array?
Use .at(-1)
instead of arr[arr.length - 1]
for cleaner and more readable code.
๐ Conclusion
Shawpnendu wasted hours writing old-style JavaScript. Donโt be like him! Use these modern JavaScript features to write cleaner, smarter, and faster code. ๐
๐ก Did you learn something new? Share this with your fellow developers! ๐
Would you like me to add more examples or refine any section? ๐
๐๏ธโโ๏ธ Discover Code Blocks From 20+ yrs JS Expert
๐ฅ Asp.net C# Developer
๐ Solution Architect
๐จโโ๏ธ Database Administrator
๐ข Speaker
๐ MCTS since 2009
Leave a Reply