Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is true == 1 and false == 0 in JavaScript?

I was reading a good book on JavaScript.

It started with:

Boolean type take only two literal values: true and false. These are distinct from numeric values, so true is not equal to 1, and false is not equal to 0.

However, I observed following:

if(1==true)   document.write("oh!!! that's true");  //**this is displayed** 

I know, that every type in JavaScript has a Boolean equivalent.

But then, what's the truth?

like image 491
Mahesha999 Avatar asked Sep 02 '12 13:09

Mahesha999


People also ask

Is false === 0 in JS?

In JavaScript “0” is equal to false because “0” is of type string but when it tested for equality the automatic type conversion of JavaScript comes into effect and converts the “0” to its numeric value which is 0 and as we know 0 represents false value. So, “0” equals to false.

Why is 1 true and 0 false JavaScript?

0 and 1 are type 'number' but in a Boolean expression, 0 casts to false and 1 casts to true . Since a Boolean expression can only ever yield a Boolean, any expression that is not expressly true or false is evaluated in terms of truthy and falsy. Zero is the only number that evaluates to falsy.

Why True == true is false in JavaScript?

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible.

Is 0 false and 1 is true?

Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.


2 Answers

It's true that true and false don't represent any numerical values in Javascript.

In some languages (e.g. C, VB), the boolean values are defined as actual numerical values, so they are just different names for 1 and 0 (or -1 and 0).

In some other languages (e.g. Pascal, C#), there is a distinct boolean type that is not numerical. It's possible to convert between boolean values and numerical values, but it doesn't happen automatically.

Javascript falls in the category that has a distinct boolean type, but on the other hand Javascript is quite keen to convert values between different data types.

For example, eventhough a number is not a boolean, you can use a numeric value where a boolean value is expected. Using if (1) {...} works just as well as if (true) {...}.

When comparing values, like in your example, there is a difference between the == operator and the === operator. The == equality operator happily converts between types to find a match, so 1 == true evaluates to true because true is converted to 1. The === type equality operator doesn't do type conversions, so 1 === true evaluates to false because the values are of different types.

like image 140
Guffa Avatar answered Sep 18 '22 21:09

Guffa


In JavaScript, == is pronounced "Probably Equals".

What I mean by that is that JavaScript will automatically convert the Boolean into an integer and then attempt to compare the two sides.

For real equality, use the === operator.

like image 21
Madara's Ghost Avatar answered Sep 18 '22 21:09

Madara's Ghost