variable set to minus number evaluates to true [duplicate] - javascript

This question already has answers here:
All falsey values in JavaScript
(4 answers)
Closed 6 years ago.
This javascript console log is confusing me.
How can x which has the value of -1 be evaluated to true in the if statement where it false the line before it? Thanks
x = -1
-1
x == true
false
if(x){console.log('yes')}
yes

When you test x in your if statement, you're not checking if x is true, you're checking if x is truthey. The rules for truthiness in javascript are:
empty strings are falsey. all other strings are truthy.
0 and NaN are falsey. all other numbers are truthy.
all objects and arrays are truthy.
null and undefined are falsey.

if(-1){console.log('yes')}
prints 'yes' in console because -1 and other numbers except 0 are truthey value, despite -1 not equal to true.
Here is a good source for extending knowledge related to javascript truthey and falsey values.

Related

Node.js- null is not equal to 0 or greater than 0 but it is greater than or equal to 0 [duplicate]

This question already has answers here:
Why is null in JavaScript bigger than -1, less than 1, but not equal (==) to 0? What is it exactly then?
(2 answers)
Closed 1 year ago.
Today I noticed that this node.js code:
console.log(null==0);
console.log(null>0);
console.log(null>=0);
Gives the output
False
False
True
This is very weird; if something is not equal to 0 or greater than 0, then why is it greater than or equal to 0? What is happening?
Because the Greater-than-or-equal operator works differently than the Equals Operator. The Greater-than-or-equal Operator converts null into 0, while the Equals Operator does not convert null to a number.
Further Explanation: https://blog.campvanilla.com/javascript-the-curious-case-of-null-0-7b131644e274

Can I get explanation for (1 + undefined) implicitly Coercion weird behavior in JS? [duplicate]

This question already has answers here:
Why is `null + 1 = 1` but `undefined + 1 = NaN`?
(3 answers)
Closed 3 years ago.
I understand the implicit coercion that happens in javascript, but i actually can't understand why this happens:
isNaN(1 + null) // false
isNaN(1 + undefined) // true
As i know that JS convert the null & undefined to 0, Or we could say consider them false value.
what is the difference here ?
Javascript converts null to 0 but undefined is converted to NaN.
check it out using the built in fucntion Number .
So in this case (1+ null) is similar to 1+ 0 which is 1
and (1+ undefined) is similar 1 + NaN which is NaN.
That's why it returns true.
console.log(Number(null));
console.log(Number(undefined));
The weird behavior here though is that Javascript consider both of these types "false" when dealing with comparison operator or as a condition in if statement and false value considered 0 after implicitly coerced...
Conclusion: Js coerce undefined & null depending on the situation. if it's in condition, it try to convert it to boolean false & if it's in an arithmetic operation it try to convert it to numbers, in this case there would be difference between them:
undefined ==> NaN
null ==> 0

How true+false===1 evaluates to true [duplicate]

This question already has answers here:
Is true == 1 and false == 0 in JavaScript?
(10 answers)
What exactly is Type Coercion in Javascript?
(9 answers)
Closed 3 years ago.
I am trying to compare true+false to 1 i.e true+false===1
In the past, I tried the following:
true===1 //false
false===0 //false
true-true===0 //true
I expect the output to be false but the actual output is true.
true and false are implicitly type casted to number when used in mathematical operation
Note:- here + is type casting it to number
console.log(+true)
console.log(+false)
console.log(true+false)
so
true + false === 1
is actually
1 + 0 === 1
When you add up two Booleans, since the Booleans do not support arithmetic operation, but logic operation only, the JS environment just converts them to Number. So 1+0===1. But if you do true ===1, this is type sensitive, it will result false. If you do true == 1, it’ll be true. Cuz this is not type sensitive, type transformation will be done by JS environment.

In Javascript console.log( (NaN||9) ); // returns 9 , why is this so? [duplicate]

This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 5 years ago.
I've noticed when I have:
console.log(( NaN||9) ); // returns 9
Why is this so?
Why, when you evaluate - put parentheses around - NaN OR 9, it chooses the 9?
It works with 0, -3, "f". Can someone please tell me what is going on in the background to cause this outcome??
In JavaScript, NaN is a "falsy" value. The || operator evaluates to either its first argument (if that is "truthy") or else to the second argument (if the first argument is "falsy"). Hence NaN || 9 evaluates to 9.
From the docs for logical operators:
Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.
and for || specifically:
Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true.
Short answer, because NaN is falsy.
Here's a list of falsy values in javascript.
The logical OR opertator || in javascript can be tricky, but once you know how it works its both straightforward and very handy. If it's left side is falsy, then it's result will be whatever it's right side is (in your case, the number 9).

Comparing NaN in javascript [duplicate]

This question already has answers here:
How do I test for NaN? [duplicate]
(2 answers)
Closed 7 years ago.
In Javascript, if we multiply a string with number we get NaN:
console.log("manas" * 5); // result is NaN
Why then does the following code result in false instead of true?
console.log("manas" * 5 == NaN) // results false
Use the isNaN function instead.
console.log(isNaN("manas" * 5));
http://www.w3schools.com/jsref/jsref_isnan.asp
NaN, not a number, is a special type value used to denote an unrepresentable value. With JavaScript, NaN can cause some confusion, starting from its typeof and all to the way the comparison is handled.
Several operations can lead to NaN as the result.
Because there are many ways to represent a NaN, it makes sense that one NaN will not be equal to another NaN.
NaN is a special numeric value which is not equal to anything including itself. To properly test for NaN you need to use isNaN function.
From specification:
Returns true if the argument coerces to NaN, and otherwise returns false.
Here is also useful note from the same ECMAScript section:
A reliable way for ECMAScript code to test if a value X is a NaN is an expression of the form X !== X. The result will be true if and only if X is a NaN.
NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.
Testing against NaN
In Javascript,
NaN == NaN //always results false.
(self comparison of NaN to itself is always false in javascript)
refer:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN
also see Is NaN equal to NaN?
Javascript is not that smart - when either side of the == operator is Nan, the whole thing evaluates as false (similar to how if leftside of || is false, it doesn't bother looking at right side)
So, even Nan == Nan will return false.
Here's a good article on Nan behaviour
http://ariya.ofilabs.com/2014/05/the-curious-case-of-javascript-nan.html

Categories

Resources