-5

So I was looking at someones code on a website and while looking around, I saw this:

var ǀ = 1;
var ǀǀ = 21;
var s = ǀǀ+(ǀ==ǀ-->ǀ+ǀ<--ǀ==ǀ)+ǀǀ;
document.writeln(s);

This somehow equates to 42? Does anyone know how they are doing this? I thought || and | where for or?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    It's lots of operators, not one. Kind of like how `1*1+1-1/1` isn't an operator. – Ry- Jan 24 '17 at 21:31
  • 4
    Hmm, your deleted [Obama one](http://stackoverflow.com/q/41838123/73226) was the [chuck Norris](http://stackoverflow.com/questions/8318911/why-does-html-think-chucknorris-is-a-color) question recycled. You've just asked a [JavaScript question](http://stackoverflow.com/questions/41838887/what-is-the-javascript-operator) that was a recycled version of http://stackoverflow.com/q/1642028/73226 where's this one from? – Martin Smith Jan 24 '17 at 21:34
  • Just walk through the var s-line. There are two variables | and ||. It is 21+( 1==1 - - > 1+1 < - - 1==1) + 21; Minus Minus 1 (--1) is +1, so it is just comparing if 1==1 +1 > 1 ; 1==1 is true 1, 1+1 is not larger 1 and so on.. So that whole part between the two brackets equals 0 and it's just 21+21. – elasticman Jan 24 '17 at 21:36
  • 1
    It evaluates to `21+(1==0>1+1<0==1)+21`. Which means: `21+(false>22` is false (because 0<2) and then `false – Andrew Li Jan 24 '17 at 21:37
  • 1
    It's new operator introduced in ES 10 :) – Anthony C Jan 24 '17 at 21:38
  • I't obviously not going to be a +84568 votes question – Alon Eitan Jan 24 '17 at 21:39
  • 1
    @MartinSmith - seems to be a '42' thing (life, the universe, etc) http://stackoverflow.com/questions/20555155/why-is-the-answer-42 http://stackoverflow.com/questions/30790761/javascript-why-returns-42 – hatchet - done with SOverflow Jan 24 '17 at 21:54
  • Please do not vandalize your posts. Once you've posted a question, it belongs to the Stack Overflow community at large (under the CC-by-SA license). If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](http://meta.stackoverflow.com/questions/323395/what-is-the-proper-rout‌​e-for-a-dissociation-request) – DavidPostill Mar 13 '17 at 14:38

4 Answers4

4

They're (ab)using a character that looks a whole lot like a vertical bar (|) such as what's used in "or" (||).

You could rewrite their code like this:

var a = 1;
var b = 21;
var s = b + (a == a-- > a + a < --a == a) + b;
document.writeln(s);

or further simplified:

var s = 21 + (1 == 1 > 0 + 0 < -1 == -1) + 21;
document.writeln(s);

// Even further simplified
var s = 21 + (true > false < true) + 21;
document.writeln(s);

// Clean up that middle case
var s = 21 + (false) + 21;
document.writeln(s);

// Coerce to a number
var s = 21 + 0 + 21;
document.writeln(s);

// Last simplification
var s = 21 + 21;
document.writeln(s);

No special operators. Just normal ones smushed together with odd-looking variable names.

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
1

ǀ and ǀǀ are (Unicode) variable names.

Space it out:

var ǀ = 1;
var ǀǀ = 21;
var s = ǀǀ + (ǀ == ǀ-- > ǀ + ǀ < --ǀ == ǀ) + ǀǀ;

Apply operator precedence and associativity:

var ǀ = 1;
var ǀǀ = 21;
var s = (ǀǀ + ((ǀ == ((ǀ-- > (ǀ + ǀ)) < --ǀ)) == ǀ)) + ǀǀ;

Apply order of evaluation:

var a = ǀǀ + ((ǀ == ((ǀ-- > (ǀ + ǀ)) < --ǀ)) == ǀ);
var s = a + ǀǀ;
var b = (ǀ == ((ǀ-- > (ǀ + ǀ)) < --ǀ)) == ǀ;
var a = ǀǀ + b;
var c = ǀ == ((ǀ-- > (ǀ + ǀ)) < --ǀ);
var b = c == ǀ;
var d = (ǀ-- > (ǀ + ǀ)) < --ǀ;
var c = ǀ == d;
var e = ǀ-- > (ǀ + ǀ);
var d = e < --ǀ;
var g = ǀ--;
var f = ǀ + ǀ;
var e = g > f;

Evaluate:

var ǀ = 1;
var ǀǀ = 21;

var g = ǀ--;
var f = ǀ + ǀ;
var e = g > f;
var d = e < --ǀ;
var c = ǀ == d;
var b = c == ǀ;
var a = ǀǀ + b;
var s = a + ǀǀ;
var ǀ = 0;
var ǀǀ = 21;

var g = 1;
var f = ǀ + ǀ;
var ǀ = 0;
var ǀǀ = 21;

var f = 0 + 0;
var e = g > f;
var ǀ = 0;
var ǀǀ = 21;

var e = 1 > 0;  // true
var d = e < --ǀ;
var ǀ = -1;
var ǀǀ = 21;

var d = true < -1;  // false
var c = ǀ == d;
var ǀ = -1;
var ǀǀ = 21;

var c = -1 == -1;  // true
var b = c == ǀ;
var ǀ = -1;
var ǀǀ = 21;

var b = true == -1;  // false
var a = ǀǀ + b;
var ǀ = -1;
var ǀǀ = 21;

var a = 21 + false;  // 21
var s = a + ǀǀ;
var ǀ = -1;
var ǀǀ = 21;

var s = 21 + 21;  // 42
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

It is a joke (a bad one). It isn't an operator. It is a collection of variable and operators put together to look intentionally confusing.

It is taking advantage of the visual similarities between ǀ (U+01C0 : LATIN LETTER DENTAL CLICK {pipe}) which is a character you can use in a variable name and | (U+007C : VERTICAL LINE {vertical bar}) which is an operator.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

This is a mixture of obfuscation, and some abuse of weak typing.

First, ǀ and ǀǀ here are just variables with funny names - names that look a bit like, but aren't actually, the common character |, which would be an operator. So let's swap them out:

var a = 1;
var b = 21;
var s = b+(a==a-->a+a<--a==a)+b;
document.writeln(s);

Now we have various operations on one line, we need to work out the precedence of the different operators, so we can break it down in the right order. The operators we have, in order of highest to lowest precedence, are:

  • (...) to give a section higher precedence
  • postfix --
  • prefix --
  • +
  • > and < (left to right)
  • == (multiple times, left to right)

So we can calculate the sum in this order:

  • the part inside the brackets is the only interesting part, the rest is 21 + (something) + 21
  • a-- gives 1, but changes a to 0 (b+(a==1>a+a<--a==a)+b)
  • --a changes a to -1 and gives -1 (b+(a==1>a+a<-1==a)+b)
  • a is now -1, so the very middle becomes -1+-1 (b+(a==1>-1+-1<-1==a)+b) which is -2 (b+(a==1>-2<-1==a)+b)
  • 1>-2 is true (b+(a==true<-1==a)+b)
  • true<-1 is false because true is treated as 1 (b+(a==false==a)+b)
  • a is -1, but false is 0, so a==false is like saying -1==0, which is false (b+(false==a)+b)
  • false==a is again false (b+(false)+b)
  • treated as a number, false is 0, so b + 0 + b is 21 + 0 + 21 is 42

Interestingly, the in-place modification of a makes this open to various interpretations. For instance, it might be that all the unmodified as are substituted as 1 (i.e. before the a++), giving some variations:

  • with a as 1, the very middle is 1+1 (b+(a==1>1+1<-1==a)+b) which is 0 (b+(a==1>2<-1==a)+b)
  • 1>2 is false (b+(a==false<-1==a)+b)
  • however, false<-1 is still false, so we get back to our original sequence
IMSoP
  • 89,526
  • 13
  • 117
  • 169