Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I break the law of non-contradiction in Javascript?

The law of non-contradiction dictates that two contradictory statements cannot both be true at the same time. That means that the expressions

(a && !a)
(a == !a)
(a === !a)

should always evaluate to a falsy value, and

(a || !a)

should always evaluate to a truthy value.

Fortunately, though, Javascript is a fun language that allows you to do all sorts of sick things. I bet someone a small fortune that it's possible to convince Javascript to break the law of non-contradiction, or, at least, convincingly make it look like it's breaking the law of non-contradiction. Now I'm trying to make all four of the above code examples give the unexpected result.

What would be a good way to go about this?

like image 235
Peter Olson Avatar asked Aug 13 '11 21:08

Peter Olson


2 Answers

The best I can do is:

[] == ![] // true

or

var a = []; 
a == !a

Of course this is really doing [] == false // true and !![] == ![] // false. It's really just a technicality.

EDIT: This is really a joke, but does work:

var a = false; var b = function() { return a = !a };
console.log(!!(b() && !b())); // true
console.log(b() == !b()); // true
console.log(b() === !b()); // true
console.log(b() || !b()); // true
like image 58
Joe Avatar answered Sep 20 '22 20:09

Joe


This one will do the trick:

var a = '0';
a == !a

(evaluates to true)

In this case, a == false and !a == false.

like image 23
mopsled Avatar answered Sep 19 '22 20:09

mopsled