Assignment operators shorthand

Posted on August 20, 2020

JavaScript has many different assignment operators that are shorthand for longer versions. += is one of the more common ones. Most other calculations also have an assignment operator.

// regular assignment assigns the right value to the variable on the left
a = b
 
// we can shorten the code if the variable is itself part of the calculation
a = a + 5    // is the same as:
a += 5
 
// these are some of the available shorthand assignment operators
a += b    // a = a + b       addition assignment
a -= b    // a = a - b       subtraction assignment
a *= b    // a = a * b       multiplication assignment
a /= b    // a = a / b       division assignment
a %= b    // a = a % b       remainder assignment
 
a &&= b   // a = a && b      logical AND assignment
a ||= b   // a = a || b      logical OR assignment
// regular assignment assigns the right value to the variable on the left
a = b
 
// we can shorten the code if the variable is itself part of the calculation
a = a + 5    // is the same as:
a += 5
 
// these are some of the available shorthand assignment operators
a += b    // a = a + b       addition assignment
a -= b    // a = a - b       subtraction assignment
a *= b    // a = a * b       multiplication assignment
a /= b    // a = a / b       division assignment
a %= b    // a = a % b       remainder assignment
 
a &&= b   // a = a && b      logical AND assignment
a ||= b   // a = a || b      logical OR assignment

Continue reading

June 23, 2020JavaScript

Splitting strings

When splitting strings, the sequence of characters we split at does not appear in the result. We can get all letters by splitting at the empty string.

Read full article
June 13, 2020JavaScript

Creating number ranges

We can create a sequence of numbers by spreading the keys of one array into another array. We can then change that range any way we like.

Read full article
July 5, 2020JavaScript

Logging as objects

This neat little trick makes values logged to the console much more readable with a minor adjustment to how we log it.

Read full article

Read all snippets →