DEV Community

Siddharth
Siddharth

Posted on

I made a new JavaScript operator!

Can you figure out how?

let x = 10;
// The new arrow operator – goes from one number to another
while (x --> 0) { // x goes to 0
  console.log(x)
}
Enter fullscreen mode Exit fullscreen mode
let x = 10; // The new arrow operator – goes from one number to another while (x --> 0) { // x goes to 0 console.log(x) }

Top comments (10)

Collapse
 
t7yang profile image
t7yang • Edited

prettier

Collapse
 
siddharthshyniben profile image
Siddharth

LOL man that was nice

Collapse
 
acelot profile image
acelot

Simple as one two three:

let x = 10;

while (x-- > 0) {
  console.log(x)
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
siddharthshyniben profile image
Siddharth • Edited

😀 you got it!

Collapse
 
_hs_ profile image
HS • Edited

I like those kind of things (the famous destroy all software videos and such). In some languages you might have range(0,10, 0.5) where last value is the step size so it give more flexibility. In others you only have things like 1..10 or 1 to 10.

Just for the laughs you might consider having step size in form of while(x -(0.5)-> 10) and when someone asks why would operator be -()-> you can respond with "because I can" or "why not" XD.

Collapse
 
mathewkurian profile image
Mathew Kurian

This is clever. Nice!

Collapse
 
tracygjg profile image
Tracy Gilmore

Keeping it simple - like it.

Collapse
 
tilkinsc profile image
Cody Tilkins

Pretty amusing. The lexer for javascript is weak in terms of enforcing code to be well written.

Collapse
 
imagineeeinc profile image
Imagineee

how did you implement this in runkit? and I really like the idea, and you can save some typing

Collapse
 
siddharthshyniben profile image
Siddharth

You mean embed? You can use liquid tags to do a lot of stuff here. That includes embedding runkit like so:

{% runkit
// hidden setup JavaScript code goes in this preamble area
const hiddenVar = 42
%}
// visible, reader-editable JavaScript code goes here
console.log(hiddenVar)
{% endrunkit %}
Enter fullscreen mode Exit fullscreen mode