DEV Community

Cover image for πŸš€ 10 Modern JavaScript Features Every Developer Should Know
David Emmanuel G
David Emmanuel G

Posted on

πŸš€ 10 Modern JavaScript Features Every Developer Should Know

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)
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Difference from ||:

console.log("" || "Fallback"); // "Fallback"
console.log("" ?? "Fallback"); // ""
Enter fullscreen mode Exit fullscreen mode

|| 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
Enter fullscreen mode Exit fullscreen mode

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");
});
Enter fullscreen mode Exit fullscreen mode

utils.js:

export function greet(name) {
  alert(`Hello, ${name}!`);
}
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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]]
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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" }
Enter fullscreen mode Exit fullscreen mode

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" }
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

To use this, your script must be in an ES Module:

<script type="module" src="topLevel.js"></script>
Enter fullscreen mode Exit fullscreen mode

🧠 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)