codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

JavaScript Quickie: Shorthand Variable Assignment

--

JavaScript Quickies are tips/tricks in 3 minutes or less. Today: Shorthand Variable Assignment.

Quick — via unsplash.com

Variables with equal values

// LONG WAY
var a = 1;
var b = 1;
var c = 1;
// SHORT WAY
var a = b = c = 1;

Variables with different values

// LONG WAY
var d = 2;
var e = 3;
var f = 4;
// SHORT WAY
var d = 2, e = 3, f = 4;

Bonus: Shorthand Assignment Operators

If you’re adding, subtracting, multiplying, dividing, or remaindering two values, there’s no need to type out the entire equation. Use these shorthands to save time and code:

Long way:

var x = 1;
var y = 2;
x = x + y;
console.log(x);
// 3

Shorthand:

var x = 1, y = 2;
x += y;
console.log(x);
// 3

Other Shorthands:

x = x + y    ->    x += y
x = x - y -> x -= y
x = x * y -> x *= y
x = x / y -> x /= y
x = x % y -> x %= y

❤ If this post was helpful, please hit the little blue heart! And don’t forget to check out my other recent articles:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Brandon Morelli

Creator of @codeburstio — Frequently posting web development tutorials & articles. Follow me on Twitter too: @BrandonMorelli

No responses yet

Write a response