Difference output for "" and " " in javascript [duplicate] - javascript

This question already has answers here:
Understanding JavaScript Truthy and Falsy
(9 answers)
Is empty string treated as falsy in javascript?
(5 answers)
Closed 4 years ago.
I have tried 2 conditions in JavaScript and output:
if(""){console.log("Called")} //No Output
if("_"){console.log("Called")} //Output: Called
What could be the possible reason for this?

The empty string is considered as a 'falsy' value, and so it's equivalent to doing:
if(false){console.log("Called")}

The empty string is equal to False if you cast it to Boolean (Type Conversion).
console.log(Boolean("")) //output: false
console.log(Boolean("somestring")) //output: true

Related

How NaN==NaN returns false? [duplicate]

This question already has answers here:
What is the intuitive reason that NaN != NaN?
(1 answer)
Why is NaN === NaN false? [duplicate]
(3 answers)
Closed 4 years ago.
NaN==NaN
returns false in javascripts. i want to know some clear explanation.
typeof NaN is number
Number == Number is return true
Then why NaN==NaN is false. Thanks advance.

Why "Hello" > "World" is returning false [duplicate]

This question already has answers here:
Why is one string greater than the other when comparing strings in JavaScript?
(5 answers)
Closed 4 years ago.
Can anyone explain me why this:
"Hello" > "World"
is returning false?
It compares them char by char, first it compares "H" > "W" and since that's false. It returns false.

What is the difference between double negation and checking if the variable is truthy directly? [duplicate]

This question already has answers here:
Why use !! to coerce a variable to boolean for use in a conditional expression?
(3 answers)
What is the difference between if(!!condition) and if(condition)
(3 answers)
Why use if (!!err)?
(3 answers)
Closed 5 years ago.
I noticed my colleagues are writing code with double negation in conditions, for example:
else if(colType == 'link'){
value = !!data ? $(data).attr('href') : '';
}
In cases like this, is there a chance that the boolean result of data can be different from !!data?

Why javascript indexOf "empty string" always returning zero [duplicate]

This question already has answers here:
indexOf empty string is zero. why?
(1 answer)
Java String.indexOf and empty Strings
(7 answers)
Closed 8 years ago.
Why javascript indexOf method always return 0 for empty string. If i used any string indexOf("") always returns zero. Any reason for this. Explain the reason briefley.
var str="jquery";
var index=str.indexOf("") \\ Always returns zero for any string.
Thanks in advance.

JavaScript: Why is NaN !== NaN? [duplicate]

This question already has answers here:
Why is NaN not equal to NaN? [duplicate]
(6 answers)
Closed 9 years ago.
What is the reasoning? I believe this is the only value not equal to itself in JavaScript.
NaN isn't equal to NaN
Use the IsNaN function to check it
See here

Categories

Resources