• Home
  • blog
  • 19+ JavaScript Shorthand Coding Techniques

19+ JavaScript Shorthand Coding Techniques

This really is a must read for any JavaScript-based developer. I have written this article as a vital source of reference for learning shorthand JavaScript coding techniques that I have picked up over the years. To help you understand what is going on I have included the longhand versions to give some coding perspective.

June 14th, 2017: This article was updated to add new shorthand tips based on ES6. If you want to learn more about the changes in ES6, sign up for SitePoint Premium and check out our screencast A Look into ES6

1. The Ternary Operator

This is a great code saver when you want to write an if..else statement in just one line.


const x = 20;
let big;

if (x > 10) {
    big = true;
} else {
    big = false;


const big = x > 10 ? true : false;

You can also nest your if statement like this:

const big = x > 10 ? " greater 10" : x < 5 ? "less 5" : "between 5 and 10";

2. Short-Circuit Evaluation Shorthand

When assigning a variable value to another variable, you may want to ensure that the source variable is not null, undefined or empty. You can either write a long if statement with multiple conditionals, or use a short-circuit evaluation.


if (variable1 !== null || variable1 !== undefined || variable1 !== '') {
     let variable2 = variable1;


const variable2 = variable1  || 'new';

Don’t believe me? Test it yourself (paste the following code in es6console):

let variable1;
let variable2 = variable1  || '';
console.log(variable2 === ''); // prints true

variable1 = 'foo';
variable2 = variable1  || '';
console.log(variable2); // prints foo

3. Declaring Variables Shorthand

It is good practice to declare your variable assignments at the beginning of your functions. This shorthand method can save you lots of time and space when declaring multiple variables at the same time.


let x;
let y;
let z = 3;


let x, y, z=3;

4. If Presence Shorthand

This might be trivial, but worth a mention. When doing “if checks”, assignment operators can sometimes be omitted.


if (likeJavaScript === true)


if (likeJavaScript)

Here is another example. If “a” is NOT equal to true, then do something.


let a;
if ( a !== true ) {
// do something...


let a;
if ( !a ) {
// do something...

5. JavaScript for Loop Shorthand

This little tip is really useful if you want plain JavaScript and not rely on external libraries such as jQuery or lodash.


for (let i = 0; i < allImgs.length; i++)


for (let img in allImgs)

Shorthand for Array.forEach:

function logArrayElements(element, index, array) {
  console.log("a[" + index + "] = " + element);
[2, 5, 9].forEach(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9

6. Short-Circuit Evaluation

Instead of writing six lines of code to assign a default value if the intended parameter is null or undefined, we can simply use a short-circuit logical operator and accomplish the same thing with just one line of code.


let dbHost;
if (process.env.DB_HOST) {
  dbHost = process.env.DB_HOST;
} else {
  dbHost = 'localhost';


const dbHost = process.env.DB_HOST || 'localhost';

7. Decimal Base Exponents

You may have seen this one around. It’s essentially a fancy way to write numbers without the trailing zeros. For example, 1e7 essentially means 1 followed by 7 zeros. It represents a decimal base (which JavaScript interprets as a float type) equal to 10,000,000.


for (let i = 0; i < 10000; i++) {}


for (let i = 0; i < 1e7; i++) {}

// All the below will evaluate to true
1e0 === 1;
1e1 === 10;
1e2 === 100;
1e3 === 1000;
1e4 === 10000;
1e5 === 100000;

8. Object Property Shorthand

Defining object literals in JavaScript makes life much easier. ES6 provides an even easier way of assigning properties to objects. If the property name is the same as the key name, you can take advantage of the shorthand notation.


const obj = { x:x, y:y };


const obj = { x, y };

9. Arrow Functions Shorthand

Classical functions are easy to read and write in their plain form, but they do tend to become a bit verbose and confusing once you start nesting them in other function calls.


function sayHello(name) {
  console.log('Hello', name);

setTimeout(function() {
}, 2000);

list.forEach(function(item) {


sayHello = name => console.log('Hello', name);

setTimeout(() => console.log('Loaded'), 2000);

list.forEach(item => console.log(item));

10. Implicit Return Shorthand


function calcCircumference(diameter) {
  return Math.PI * diameter


calcCircumference = diameter => Math.PI * diameter;

11. Default Parameter Values

You can use the if statement to define default values for function parameters. In ES6, you can define the default values in the function declaration itself.

Continue reading %19+ JavaScript Shorthand Coding Techniques%