I Love JavaScript, but...

I really do love JavaScript. It's an incredible tool to build applications that people everywhere can use. However, there are tons of weird things that you can, but shouldn't, do with it.

This little article is meant to showcase what is sometimes referred to as the "goes down to" operator.

// 'Goes down to' operator!!

var n = 10;
while (n --> 1) {
    console.log('Number: ' + n);
}

This little guy --> is kind of a silly thing. What it really is is a combination of decrement operator -- and greater than conditional >. If you were to write this normally, you'd have:

var n = 10;
while (n-- > 1) {
    console.log('Number: ' + n);
}

What this is doing is checking to see if n is greater than 1. If it is, it decrements it with the -- operator, and then runs the loop body.

You can use its opposite operator, the "What the.."1 operator ++<, as in this example:

var n = 1;
while (n ++< 10) {
    console.log('Number: ' + n);
}

What this is doing is comparing n to 10. If it is less, it will increment n and then run the loop body.

CAVEAT You really should never do this in a production site. In fact, you probably should never do this ever. It's not usually clear, and it can be difficult to maintain.

1 I made this name up. Feel free to call it whatever you like. On second thought, just don't ever use this. Please. I beg of you.

Anna Poulakos

I'm the Solution Architect at a top ed tech company.
United States