← Back to list

7 JavaScript Tricks That Cut My Code in Half

One-liners and patterns that make everyday coding cleaner

Babar saad in JavaScript in Plain English · 2025-09-07 02:13 · 20 claps · 1.2 min read paywalled
#coding #productivity #javascript #web-development #one-liners
Open on Medium ↗
Wiki topics: 💻 · Programming 🌐 · Web Development ⏱️ · Productivity

7 JavaScript Tricks That Cut My Code in Half

One-liners and patterns that make everyday coding cleaner

Seven practical JavaScript tricks and one-liners that simplify your code and make it easier to maintain.

1 — Remove Duplicates from an Array

const nums = [1, 2, 2, 3, 4, 4];
const unique = [...new Set(nums)];
console.log(unique); // [1, 2, 3, 4]

2 — Swap Two Variables

let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b); // 2 1

3 — Flatten Nested Arrays

const arr = [1, [2, [3, [4]]]];
console.log(arr.flat(Infinity)); // [1, 2, 3, 4]

4 — Short-Circuit Default Values

const username = input || "Guest";

5 — Destructure Objects in Function Params

function greet({ name, age }) {
  console.log(`Hello ${name}, age ${age}`);
}
greet({ name: "Ali", age: 25 });

6 — Convert to Boolean Fast

console.log(!!"hello"); // true
console.log(!!0);       // false

7 — Optional Chaining for Safety

const user = { profile: { email: "test@mail.com" } };
console.log(user?.profile?.email); // test@mail.com
console.log(user?.settings?.theme); // undefined

A message from our Founder

Hey, Sunil here. I wanted to take a moment to thank you for reading until the end and for being a part of this community.

Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? We don’t receive any funding, we do this to support the community. ❤️

If you want to show some love, please take a moment to follow me on LinkedIn, TikTok, **Instagram. You can also subscribe to our [weekly newsletter](https://newsletter.plainenglish.io/)**.

And before you go, don’t forget to clap and follow the writer️!


메타데이터
post_id
c9cfb80b247a
slug
7-javascript-tricks-that-cut-my-code-in-half-c9cfb80b247a
url
https://javascript.plainenglish.io/7-javascript-tricks-that-cut-my-code-in-half-c9cfb80b247a
canonical_url
https://javascript.plainenglish.io/7-javascript-tricks-that-cut-my-code-in-half-c9cfb80b247a
author_url
https://medium.com/@sa82912045
status
ok
fetched_at
2026-06-09 15:37:30