JavaScript has rapidly evolved over the past few years, offering developers cleaner syntax, better performance patterns, and smarter tools to write maintainable code. In this post, weβll explore 10 powerful JavaScript features you should know β complete with real-world illustrations and in-depth explanations.
1. π Optional Chaining (?.
)
What it is:
Safely access nested object properties without checking each level manually.
Code Example:
const user = {
profile: {
name: "Alice",
address: {
city: "Bangalore"
}
}
};
console.log(user.profile?.address?.city); // "Bangalore"
console.log(user.profile?.phone?.number); // undefined (no error)
Why it matters:
No more TypeError: Cannot read property '...' of undefined
. This is extremely helpful when working with APIs where certain properties may or may not exist.
2. π° Nullish Coalescing Operator (??
)
What it is:
Provides a fallback value only when the left-hand operand is null
or undefined
.
Code Example:
const responseTitle = null;
const title = responseTitle ?? "Default Title";
console.log(title); // "Default Title"
Difference from ||
:
console.log("" || "Fallback"); // "Fallback"
console.log("" ?? "Fallback"); // ""
||
treats ""
, 0
, and false
as falsy, while ??
is more precise β it only checks for null
or undefined
.
3. π§© Destructuring with Default Values
What it is:
Extract object properties and assign default values if they are undefined.
Code Example:
const settings = {
theme: "dark"
};
const { theme, fontSize = 14 } = settings;
console.log(theme); // "dark"
console.log(fontSize); // 14
Use case:
Great for setting up default component props, function parameters, or user configurations.
4. π¦ Dynamic import()
What it is:
Load JavaScript modules only when needed, making your app faster and modular.
Code Example:
document.getElementById("loadBtn").addEventListener("click", async () => {
const { greet } = await import('./utils.js');
greet("Dev.to user");
});
utils.js
:
export function greet(name) {
alert(`Hello, ${name}!`);
}
Why it matters:
Improve performance via code splitting. Only load large libraries or tools when the user needs them.
5. π Rest and Spread Operators (...
)
What it is:
- Spread: Expands arrays or objects
- Rest: Gathers remaining items into an array or object
Code Example: Spread
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];
console.log(arr2); // [1, 2, 3, 4, 5]
Code Example: Rest
function showInfo(name, ...skills) {
console.log(`Name: ${name}`);
console.log(`Skills: ${skills.join(", ")}`);
}
showInfo("Dev", "JS", "React", "Node");
// Name: Dev
// Skills: JS, React, Node
6. π§± Array flat()
and flatMap()
What it is:
Simplify working with nested arrays.
Code Example: flat()
const nested = [1, [2, [3, [4]]]];
console.log(nested.flat(2)); // [1, 2, 3, [4]]
Code Example: flatMap()
const words = ["hello", "world"];
const letters = words.flatMap(word => word.split(""));
console.log(letters); // ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
7. π Object.fromEntries()
What it is:
Convert an array of key-value pairs into an object.
Code Example:
const entries = [["id", 1], ["name", "Dev"]];
const user = Object.fromEntries(entries);
console.log(user); // { id: 1, name: "Dev" }
Common pattern:
const obj = { a: 1, b: 2 };
const inverted = Object.fromEntries(
Object.entries(obj).map(([key, val]) => [val, key])
);
console.log(inverted); // { 1: "a", 2: "b" }
8. π Logical Assignment Operators (&&=
, ||=
, ??=
)
What it is:
Shorthand for combining assignment with logic.
Code Example:
let userSettings = null;
userSettings ??= { theme: "dark" };
console.log(userSettings); // { theme: "dark" }
let counter = 0;
counter ||= 10;
console.log(counter); // 10 (because 0 is falsy)
This makes conditional assignments more expressive and clean.
9. π Private Class Fields (#
syntax)
What it is:
Declare truly private fields in JavaScript classes.
Code Example:
class Counter {
#count = 0;
increment() {
this.#count++;
console.log(this.#count);
}
get value() {
return this.#count;
}
}
const c = new Counter();
c.increment(); // 1
console.log(c.value); // 1
// console.log(c.#count); β SyntaxError
No one can access #count
from outside the class β strong encapsulation FTW.
10. β³ Top-Level await
What it is:
Use await
directly at the top level of a module (no more wrapping in IIFE).
Code Example:
// topLevel.js (ensure it's an ES module)
const response = await fetch("https://jsonplaceholder.typicode.com/posts/1");
const data = await response.json();
console.log(data.title);
To use this, your script must be in an ES Module:
<script type="module" src="topLevel.js"></script>
π§ Final Thoughts
Modern JavaScript isn't just about new syntax β it's about writing less code, with more clarity, and greater power. Whether you're building front-end apps or back-end services, adopting these features will help you write cleaner, safer, and faster code.
π¬ Which feature do you use the most? Which one are you excited to try? Letβs discuss in the comments!
#javascript #webdev #programming #beginners #esnext
Top comments (0)