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

Why 90% of JavaScript Devs Donโ€™t Use These Game-Changing Features ๐ŸŽฏ

Shawpnendu Bikash Maloroy

March 7, 2025
JavaScript Devs Dont Use These Game-Changing Features
Spread the love

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:

How to handle optional properties in code
  • ?. checks if the previous property exists.
  • If user.profile doesnโ€™t exist, it returns undefined 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:

Choose the appropriate operator for handling falsy values in JavaScript.
  • || sees 0 as falsy, so it replaces it with 10.
  • ?? only checks for null or undefined, keeping 0 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 โœ…
Safe JS Code

๐Ÿ’ก Tip:

Object.hasOwn() is more reliable when working with inherited properties or prototype chains.

๐Ÿ”ฅ 4. The at() Method for Arrays

JS 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.

Deep Copying Objects using JS

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)

Note

Works in modern browsers, but not in older ones.

๐Ÿ’ก Tip:

Use structuredClone() instead of third-party libraries for deep copying objects.

๐Ÿ“Š Comparison Table

FeatureOld MethodNew MethodWhy It’s Better?
Optional ChainingLong if conditions?. operatorCleaner & safer code
Nullish CoalescingChecks all falsy values?? only checks null & undefinedAvoids 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? ๐Ÿš€

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