Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does using "0" with the ternary operator return the first value?

I was playing around with JSconsole and found something strange. The value of "0" is false

"0" == false
=> true

The value of false when used in ternary returns the second value

false ? 71 : 16
=> 16

However the value "0" which equals false when used in ternary returns the first value.

"0" ? 8 : 10
=> 8

However, if you use 0 as the value, it returns the second value

0 ? 4 : 5
=> 5

0 == "0"
=> true

I'm afraid this doesn't make sense to me.

like image 538
Richard Hamilton Avatar asked Jun 05 '15 16:06

Richard Hamilton


People also ask

Does ternary operator return a value?

The ternary operator is used to return a value based on the result of a binary condition. It takes in a binary condition as input, which makes it similar to an 'if-else' control flow block. It also, however, returns a value, behaving similar to a function.

What does ternary operator return in C?

This operator returns one of two values depending on the result of an expression. If "expression-1" is evaluated to Boolean true, then expression-2 is evaluated and its value is returned as a final result otherwise expression-3 is evaluated and its value is returned as a final result.

How do I return with ternary operator?

In a ternary operator, we cannot use the return statement. The ternary operator requires expressions but not code.

Why we should not use ternary operator?

They simply are. They very easily allow for very sloppy and difficult to maintain code. Very sloppy and difficult to maintain code is bad. Therefore a lot of people improperly assume (since it's all they've ever seen come from them) that ternary operators are bad.


2 Answers

Non-empty string is considered as truth value in conditional statements, conditional expressions and conditional constructs.

But when you compare a string with a number with ==, some conversion will take place.

When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.

And == don't have the Transitive Property of Equality:

you can't say if a == b, b == c, then a == c.

An example will be:

"0" == false // true
false == "\n" //true

and guess the result of "0" == "\n"? Yes, the result is false.

like image 74
xdazz Avatar answered Sep 18 '22 11:09

xdazz


"0" is a string of length>0 which is true. Try

0 ? 8 : 10

and see. It will return 10.

== does type conversion and hence when you do

"0" == false

it returns true. When you do

0 == "0" //true

It also returns true as again type conversion is taking place. Even though one is a number and the other one is a string it returns true. But if you use ===, no type conversion is done and 0 === "0" will return false.

A nice explanation of == & === is given here.

From the docs:

The equality operator(==) converts the operands if they are not of the same type, then applies strict comparison.

The identity operator(===) returns true if the operands are strictly equal with no type conversion.

like image 37
Zee Avatar answered Sep 20 '22 11:09

Zee