13 Essential JavaScript Code Optimizations to Write Cleaner, Faster Code
This article presents thirteen practical JavaScript optimization techniques—including concise conditional checks, streamlined if‑else statements, combined variable declarations, destructuring assignments, logical‑AND shortcuts, arrow functions, ternary calls, object‑based switches, default parameters, spread operators, template literals, and object shorthand—to help developers write cleaner, more efficient code.
1. Multiple expressions in a single if
Replace a long chain of OR conditions with an array and includes for brevity.
if (x === 'abc' || x === 'def' || x === 'ghi' || x === 'jkl') {
// logic
}
if (['abc', 'def', 'ghi', 'jkl'].includes(x)) {
// logic
}2. Concise if‑else
When the logic is simple, use a ternary operator or direct boolean assignment.
let test: boolean;
if (x > 100) {
test = true;
} else {
test = false;
}
// short
let test = (x > 10) ? true : false;
// even shorter
let test = x > 10;3. Merge variable declarations
Declare multiple variables of the same type in a single statement.
let test1;
let test2 = 1;
// short
let test1, test2 = 1;4. Merge variable assignments
Use array destructuring to assign several values at once.
let test1, test2, test3;
test1 = 1;
test2 = 2;
test3 = 3;
// short
let [test1, test2, test3] = [1, 2, 3];5. Logical AND (&&) operator
Call a function only when a condition is true using &&.
if (test1) {
callMethod();
}
// short
test1 && callMethod();6. Arrow functions
Replace traditional function syntax with an arrow function for brevity.
function add(a, b) {
return a + b;
}
// short
const add = (a, b) => a + b;7. Short function invocation
Use a ternary expression to choose and invoke a function in one line.
const fun1 = () => console.log('fun1');
const fun2 = () => console.log('fun2');
let test = 1;
if (test == 1) {
fun1();
} else {
fun2();
}
// short
(test === 1 ? fun1 : fun2)();8. Switch shorthand
Store case handlers in an object and invoke them via property lookup.
switch (data) {
case 1:
test1();
break;
case 2:
test2();
break;
case 3:
test();
break;
}
// short
const data = {
1: test1,
2: test2,
3: test
};
data[something] && data[something]();9. Default parameter values
Specify defaults directly in the function signature.
function add(test1, test2) {
if (test1 === undefined) test1 = 1;
if (test2 === undefined) test2 = 2;
return test1 + test2;
}
// short
const add = (test1 = 1, test2 = 2) => test1 + test2;10. Spread operator
Use ... to merge or copy arrays concisely.
// merge arrays (long)
const data = [1, 2, 3];
const test = [4, 5, 6].concat(data);
// merge arrays (short)
const data = [1, 2, 3];
const test = [4, 5, 6, ...data];
// copy array (long)
const test1 = [1, 2, 3];
const test2 = test1.slice();
// copy array (short)
const test1 = [1, 2, 3];
const test2 = [...test1];11. Template literals
Combine strings and variables with back‑ticks instead of concatenation.
// long
const welcome = 'Hi ' + test1 + ' ' + test2 + '.';
// short
const welcome = `Hi ${test1} ${test2}`;12. Object shorthand
When property names match variable names, omit the explicit key.
let test1 = 'a';
let test2 = 'b';
// long
let obj = { test1: test1, test2: test2 };
// short
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); // 1Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
