13 JavaScript Tricks to Write Cleaner, Faster Code

Discover 13 practical JavaScript shortcuts—from using array includes in conditionals to leveraging the spread operator and object shorthand—that dramatically simplify code, improve readability, and boost performance for modern web development.

JavaScript
JavaScript
JavaScript
13 JavaScript Tricks to Write Cleaner, Faster Code

1. Multiple expressions in an if statement

Replace a long chain of OR conditions with an includes check on an array.

if (['abc', 'def', 'ghi', 'jkl'].includes(x)) {
  // logic
}

2. Simplify if‑else

When the logic is simple, use a ternary operator or direct boolean assignment.

let test = (x > 10) ? true : false;
// or even
let test = x > 10;

3. Merge variable declarations

Declare variables of the same type in a single statement.

let test1, test2 = 1;

4. Merge variable assignments

Use array destructuring to assign multiple values at once.

let [test1, test2, test3] = [1, 2, 3];

5. && operator for conditional calls

Execute a function only when a variable is truthy.

test1 && callMethod();

6. Arrow functions

Replace a traditional function with a concise arrow function.

const add = (a, b) => a + b;

7. Short function invocation

Choose between two functions with a ternary expression and invoke the result immediately.

(test === 1 ? fun1 : fun2)();

8. Switch shorthand

Map cases to functions in an object and call them dynamically.

const data = {
  1: test1,
  2: test2,
  3: test
};
data[something] && data[something]();

9. Default parameter values

Define defaults directly in the function signature.

const add = (test1 = 1, test2 = 2) => test1 + test2;

10. Spread operator

Merge arrays and copy arrays succinctly.

// merge arrays
const data = [1, 2, 3];
const test = [4, 5, 6, ...data];

// copy array
const test1 = [1, 2, 3];
const test2 = [...test1];

11. Template strings

Build strings with embedded expressions.

const welcome = `Hi ${test1} ${test2}`;

12. Object shorthand

When property names match variable names, omit the key.

let obj = {test1, test2};

13. Find max and min in an array

Use Math.max and Math.min with the spread operator.

const arr = [1, 2, 3];
Math.max(...arr); // 3
Math.min(...arr); // 1
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptCode Optimizationes6Tipsshort syntax
JavaScript
Written by

JavaScript

Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.