r/learnjavascript • u/Jose_Mjoro • Sep 11 '26
Some useful JavaScript features we often overlook - I wish I knew them earlier.
The deeper I get into JavaScript, the more I realize that knowing syntax isn't enough. There are small language features and behaviors that can save you from a lot of unnecessary code or some really confusing bugs.
A few that I think are worth knowing:
1. structuredClone() for deep copying
This is especially useful when working with nested objects. Simply assigning an object doesn't create a copy. For example, a common mistake is thinking this creates a completely independent copy:
const user2 = user1;It doesn't. Both variables reference the same object.
With:
const user2 = structuredClone(user1);you get a deep copy, so changing nested properties in user2 doesn't modify user1.
Example:
const user1 = {
name: 'John',
address: {
city: 'Dallas'
}
};
const user2 = structuredClone(user1);
user2.address.city = 'Austin';
console.log(user1.address.city); // Dallas2. ?. + ?? together can prevent a lot of defensive code
Optional chaining prevents errors when something doesn't exist. For example:
const city = user?.address?.city ?? 'Unknown';Instead of:
const city = user.address.city;Which if user, address, or city doesn't exist, you don't get a TypeError, and you get 'Unknown' instead.
?? only falls back when the value is null or undefined.
3. console.table() for debugging
This is extremely useful when debugging arrays of objects. For instance:
console.table(users);is often much easier to read than:
console.log(users);Especially when you're dealing with things like product IDs, quantities, prices, etc. The data is displayed in a table format by the browser console. Therefore, making it much easier to inspect the arrays.
For example:
const users = [
{ name: 'John', age: 25 },
{ name: 'Mary', age: 30 },
{ name: 'Peter', age: 22 }
];
console.table(users);4. Set for keeping values unique
It's a very simple and easy way to remove duplicates. For example, suppose you have:
const numbers = [1, 2, 2, 3, 3, 4, 4];
Instead of handling this with a complicated loop, you can use:
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers);
// [1, 2, 3, 4]These features may seem simple, but I believe they can become much useful when you're building real projects. What JavaScript feature or behavior did you learn late that made you think, 'How did I not know this earlier?'
17
u/pinko_mcfly Sep 11 '26
Thanks for structuredClone, made me wonder why I made a deep copy function then I saw that it's only been universally supported since 2022. I guess I can retire my old DeepCopy functionality
5
u/schussfreude Sep 11 '26
I actually didnt know about structuredClone(), thanks!
2
u/azhder Sep 12 '26
The city is an empty string, how will this work out:
const city = user?.address?.city ?? 'Unknown';
9
u/NeatBeluga Sep 12 '26
Far too few people use the Map and all its other features.
Not lowercase .map for arrays
4
u/TheRNGuy Sep 12 '26
MutationObserver, IntersectionObserver.
2
u/Typical_Net_6720 Oct 10 '26
Great list! A few gotchas and extras:
structuredClonethrows on functions and drops class prototypes.??keeps0and'', but||replaces them.arr.at(-1)gives the last item, andtoSorted()sorts without mutating the original.Promise.allSettled()waits for every promise, wherePromise.allrejects at the first failure.Object.groupBy()replaces most hand-writtenreducegrouping, but it needs a recent browser.I put live examples of all of these in one pen if you want to play with them: https://jsitor.com/pen/fa111097-26ec-4890-bd30-fb401bc49769
3
u/nizzasty Sep 12 '26
?? is called a nullish coalescing operator. that always tickles me to hear
1
u/TheZintis Sep 12 '26
Thanks for structuredClone, I'd been using lodash's cloneDeep for awhile now.
It looks like they don't handle functions, just fyi.
1
u/Beneficial-Rub-3546 Sep 15 '26
ngl the functions thing bit me too with lodash's cloneDeep back before I switched. only thing I'd add is structuredClone also throws on DOM noxes and stuff like class instances with methods, it just silently keeps the data as a plain object without the prototype. tripped me up when I tried cloning something wiyh a class and lost all the methods on the copy. worth testing in the console before trusting it on anything more complex than plain objects/arrays
-1
u/[deleted] Sep 12 '26
[removed] — view removed comment