JavaScript is a versatile programming language with a wide range of features and functionalities. Here are some hidden gems in JavaScript, along with examples of how they can be used:
1. Optional Chaining (?.):
Optional chaining allows you to access nested properties or methods without worrying about whether an intermediate property or method exists. It helps prevent “TypeError: Cannot read property ‘x’ of undefined” errors.
Here’s an example:
const user = {
name: "John",
address: {
city: "New York"
}
};
console.log(user.address?.city); // Output: New York
console.log(user.address?.street); // Output: undefined
2. Nullish Coalescing Operator (??):
The nullish coalescing operator allows you to provide a default value when dealing with null or undefined values. It’s handy when you want to fall back to a default value if a variable is null or undefined. Here’s an example:
const name = null;
const defaultName = "John";
const displayName = name ?? defaultName;
console.log(displayName); // Output: John
3. Object.fromEntries():
The Object.fromEntries() method transforms a list of key-value pairs into an object. It takes an array of key-value pairs and returns an object with those keys and values.
Here’s an example:
const entries = [
['name', 'John'],
['age', 30],
['city', 'New York']
];
const obj = Object.fromEntries(entries);
console.log(obj); // Output: { name: 'John', age: 30, city: 'New York' }
4. Array.prototype.flatMap():
The flatMap() method combines the mapping and flattening of arrays. It applies a function to each element of an array and then flattens the result into a new array. Here’s an example:
const numbers = [1, 2, 3, 4];
const doubled = numbers.flatMap(num => [num, num * 2]);
console.log(doubled); // Output: [1, 2, 2, 4, 3, 6, 4, 8]
5. Spread Operator:
The spread operator in JavaScript is denoted by three dots (...
). It allows you to spread the elements of an iterable object, such as an array or a string, into another array, function argument, or object. It provides a concise way to clone arrays, concatenate arrays, and combine objects.
Here’s an example that demonstrates the usage of the spread operator:
// Example 1: Cloning an array
const originalArray = [1, 2, 3];
const clonedArray = [...originalArray];
console.log(clonedArray); // Output: [1, 2, 3]
console.log(originalArray === clonedArray); // Output: false (different references)
// Example 2: Concatenating arrays
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const combinedArray = [...array1, ...array2];
console.log(combinedArray); // Output: [1, 2, 3, 4, 5, 6]
// Example 3: Spreading a string into an array
const str = 'Hello';
const charArray = [...str];
console.log(charArray); // Output: ['H', 'e', 'l', 'l', 'o']
// Example 4: Spreading elements as function arguments
const numbers = [1, 2, 3];
const sum = (a, b, c) => a + b + c;
const result = sum(...numbers);
console.log(result); // Output: 6
// Example 5: Combining objects
const obj1 = { name: 'John' };
const obj2 = { age: 30 };
const combinedObject = { ...obj1, ...obj2 };
console.log(combinedObject); // Output: { name: 'John', age: 30 }
These are just a few hidden gems in JavaScript that can enhance your code and make it more concise and expressive. Experiment with them to discover more about their capabilities and how they can simplify your programming tasks.

Hi, I’m Vinod Rajbhar, a full-stack JavaScript developer with over 14 years of experience building scalable, high-performance apps using React, Node.js, and WebRTC. I specialize in turning buggy ideas into clean, production-ready solutions. Outside of work, I’m a proud father of two ‘feature-rich chaos modules’ and married to a wonderfully strict teacher—basically, I debug code at work and behavior at home!