Yahya Saeed Dev

JavaScript

Modern JavaScript Tricks That Save Time

By Yahya Saeed · 4 min read · 5 views

Modern JavaScript Tricks That Save Time

Modern JavaScript Tricks That Save Time

Every experienced JavaScript developer has a collection of small tricks that make coding faster and cleaner.

These aren't complicated algorithms or obscure language features.

They're practical techniques that save minutes every day.

And those minutes quickly become hours over the course of a year.

Modern JavaScript includes dozens of features designed to reduce boilerplate, prevent bugs, and improve readability.

If you're still writing JavaScript the same way you did a few years ago, you're probably doing more work than necessary.

Let's explore the JavaScript tricks every modern developer should know in 2026.

Use Optional Chaining Instead of Long Checks

Instead of writing:

if (user && user.profile && user.profile.address) {
  console.log(user.profile.address.city);
}

Simply write:

console.log(user?.profile?.address?.city);

Cleaner.

Shorter.

Much easier to read.

Use Nullish Coalescing for Better Defaults

Many developers still use:

const username = user.name || "Guest";

The problem is that values like:

  • 0

  • false

  • ""

will also trigger the default.

Instead use:

const username = user.name ?? "Guest";

Only null and undefined use the fallback value.

Copy Objects Without Mutating Them

Instead of changing an object directly:

user.role = "Admin";

Create a new object:

const updatedUser = {
  ...user,
  role: "Admin",
};

This approach works especially well in React state management.

Remove Duplicate Values Instantly

Instead of writing loops:

const unique = [...new Set(numbers)];

Example:

const numbers = [1, 2, 2, 3, 4, 4];

const unique = [...new Set(numbers)];

Result:

[1, 2, 3, 4]

Simple and incredibly useful.

Get the Last Item Easily

Old approach:

array[array.length - 1]

Modern approach:

array.at(-1)

It's cleaner and easier to understand.

Swap Variables Without a Temporary Variable

Instead of:

let temp = a;
a = b;
b = temp;

Use destructuring:

[a, b] = [b, a];

One line.

No temporary variable.

Shorten Object Creation

Instead of:

const user = {
  name: name,
  age: age,
};

Write:

const user = {
  name,
  age,
};

JavaScript automatically understands the property names.

Destructure Function Parameters

Instead of:

function greet(user) {
  console.log(user.name);
}

Write:

function greet({ name }) {
  console.log(name);
}

Cleaner and more readable.

Use Default Parameters

Instead of:

if (!name) {
  name = "Guest";
}

Use:

function greet(name = "Guest") {
  return `Hello ${name}`;
}

Much simpler.

Convert Strings to Numbers Quickly

Instead of:

Number(value)

You can also use:

+value

Example:

const age = +"25";

Simple and fast.

Flatten Nested Arrays

Instead of loops:

const flat = nested.flat();

Or:

const flat = nested.flat(2);

Perfect when working with nested data.

Use Object.entries()

Instead of looping manually:

for (const [key, value] of Object.entries(user)) {
  console.log(key, value);
}

This is cleaner than older approaches.

Group Array Operations

Instead of multiple loops:

users
  .filter(user => user.active)
  .map(user => user.name)
  .sort();

Method chaining keeps transformations readable.

Use includes() Instead of indexOf()

Old:

if (roles.indexOf("admin") !== -1)

Modern:

if (roles.includes("admin"))

Much easier to understand.

Clone Objects Properly

Instead of:

JSON.parse(JSON.stringify(obj))

Use:

structuredClone(obj);

This handles many modern JavaScript data types correctly.

Dynamic Imports

Load code only when needed.

const analytics = await import("./analytics");

This reduces initial bundle size and improves performance.

Use Promise.all()

Instead of waiting for requests one at a time:

const users = await fetchUsers();
const posts = await fetchPosts();

Run them together:

const [users, posts] = await Promise.all([
  fetchUsers(),
  fetchPosts(),
]);

Parallel execution is often much faster.

Prefer map() Over Manual Loops

Instead of:

const names = [];

for (const user of users) {
  names.push(user.name);
}

Write:

const names = users.map(user => user.name);

Cleaner and more expressive.

Use filter(Boolean)

Remove falsy values instantly.

const values = [1, null, "", 5, undefined];

const cleaned = values.filter(Boolean);

Useful when cleaning arrays.

Use Object.fromEntries()

Convert arrays back into objects.

const user = Object.fromEntries(entries);

Great when transforming data.

Use Logical Assignment Operators

Instead of:

if (user.name == null) {
  user.name = "Guest";
}

Write:

user.name ??= "Guest";

Modern JavaScript provides:

  • ??=

  • ||=

  • &&=

These operators simplify assignments.

Use Template Literals Everywhere

Instead of:

"Hello " + name

Use:

`Hello ${name}`

They're easier to read and support multi-line strings.

Use Rest Parameters

Instead of the old arguments object:

function sum(...numbers) {
  return numbers.reduce((a, b) => a + b, 0);
}

This works with arrays naturally.

Use Early Returns

Instead of deeply nested conditions:

if (!user) {
  return;
}

if (!user.active) {
  return;
}

console.log(user.name);

Early returns reduce nesting and improve readability.

Destructure Arrays

Instead of:

const first = colors[0];
const second = colors[1];

Write:

const [first, second] = colors;

Simple and concise.

Use toSorted() Instead of sort()

Instead of mutating the original array:

const sorted = numbers.toSorted();

Unlike sort(), this returns a new array.

Perfect for immutable code.

Use replaceAll()

Instead of regular expressions:

text.replaceAll("-", " ");

Much easier for simple replacements.

Use Console Shortcuts Wisely

Useful debugging helpers include:

console.table(users);

console.time("API");

console.timeEnd("API");

console.group("User");

console.groupEnd();

These make debugging much more efficient.

Tricks Worth Learning First

If you're still improving your JavaScript skills, master these tricks first:

  • Optional Chaining

  • Nullish Coalescing

  • Destructuring

  • Spread Operator

  • Async/Await

  • Promise.all()

  • Set

  • Map

  • Template Literals

  • Array Methods

You'll use them almost every day.

Why These Tricks Matter

Each trick may save only a few seconds.

But over hundreds of coding sessions they make a significant difference.

They help you:

  • Write less code

  • Avoid common bugs

  • Improve readability

  • Reduce duplication

  • Build applications faster

  • Make maintenance easier

That's why experienced developers rely on them constantly.

Final Thoughts

Modern JavaScript is full of features designed to help developers write cleaner, faster, and more reliable code.

You don't need to memorize every trick overnight.

Start with the ones you'll use every day.

As they become second nature, your code will naturally become shorter, easier to understand, and more professional.

The best developers aren't the ones who write the most code.

They're the ones who solve problems with the simplest and clearest solutions.

Keep reading

Related Posts

Trending

Popular Posts

Comments

No approved comments yet.