Behaviour of and operator in javascript [duplicate] - javascript

This question already has answers here:
Javascript AND operator within assignment
(7 answers)
Closed 7 years ago.
I am checking for the values of AND operator in javascript, the below code for some reason returning 0. Can someone explain actual behavior of AND operator here?
var result = 88 && 6 && 0 && null && 9;
alert(result);

&& evaluates as the left hand side if the left hand side is false, otherwise it evaluates as the right hand side.
(88) && (6 && 0 && null && 9)
88 is true, so the right hand side is evaluated.
(6) && (0 && null && 9)
6 is true, so the right hand side is evaluated.
(0) && (null && 9)
0 is false, so it is the result.

Related

Chain equality operator in javascript [duplicate]

This question already has answers here:
Can I use chained comparison operator syntax? [duplicate]
(6 answers)
Why does (0 < 5 < 3) return true?
(14 answers)
Closed 1 year ago.
I have a question about how chain equality works in JavaScript.
For example in python if you have:
a, b = 1, 2
a == b == False //False
Because it converts to:
(a == b) and (b == False)
So, finally it is False.
But when I try this in js:
console.log(1==2==false) // true
I got "true". I don't know why and how it is worked in js.
could you please help me out?
Reading left to right:
1==2 is false
false==false is true
in the code 1 == 2 == false
we read it as 1 == 2 == false
so basically 1 == 2 is false
and fasle == false is true

Checking for `null` in JavaScript [duplicate]

This question already has answers here:
Why `null >= 0 && null <= 0` but not `null == 0`?
(6 answers)
Javascript comparison question (null >= 0) [duplicate]
(4 answers)
Closed 2 years ago.
I am a little confused by this:
0 > null // gives me `false`
0 === null // gives me `false` - also
0 == null // is `false`
How come 0 >= null becomes true?
Comparisons convert null to a number, treating it as 0. That’s why 0 >= null is true.
Example:
0 === Number(null) // true
0 >= Number(null) // true
Edit:
There's a difference on how null is handled in comparisons like >=, <=, >, < and how it is handled in equality checks like ===, == .

Why does this function return 0 [duplicate]

This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
All falsey values in JavaScript
(4 answers)
Closed 5 years ago.
I am trying to figure out why this javascript function returns 0 when this.position.startOffset value is 0 but not when the value is a number other than 0.
ready: function() {
return (this.position.startOffset
&& this.position.endOffset
&& this.position.representation.trim().length >= 0
&& this.text.id
&& this.user.id
&& this.concept);
}
The && chain will stop evaluating at the first non-truthy (falsy) value and return it. Since 0 is falsy it is returned when it is encountered. If no falsy value is encountered then the last value is returned:
var a = 55 && [] && 0 && false && true; // yeild 0 as it is the first falsy value to encounter
console.log("a:", a);
var b = 30 && true && [] && "haha"; // yeild "haha" as it is the last value (no falsy value encountered)
console.log("b:", b);
Falsy values are:
null
undefined
the empty string '' or ""
the number 0
boolean false
NaN
This happens due to how javascript handles numbers in boolean expressions.
If the first parameter in a and (&&) statement returns 0, then the right parameters will not be evaluated and 0 will be returned.

In javascript, 5 || 0 and 0 || 5 returns 5 [duplicate]

This question already has answers here:
What does the || operator do?
(4 answers)
Proper use of ||
(3 answers)
Javascript-like || in php [closed]
(1 answer)
Closed 8 years ago.
As the title says everything. 5 || 0 and 0 || 5 returns 5 in JavaScript. Why does this happen and what does two || means in javascript?
It's a boolean or, and 5 evaluates to truthy. If you want to force your types to boolean you should use the !! (double negation) like so,
!!(5 || 0)
|| is a boolean or.
5 == true
0 == false
So, 5 || 0 = 5
The || is a synonym for the logical OR
So the statement ANY_VALUE || ANY_OTHER_VALUE means that if the first value is truthy then return that else return the second value

Should I use == or === In Javascript? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Difference between == and === in JavaScript [duplicate]
(2 answers)
Closed 8 years ago.
I am learning Javascript with codecademy, and I was doing some comparisons, and for my code I did:
`console.log(1 == 2)`
and it returned False.
I also did:
`console.log(2*2 === 3)`
and that also returned False.
To check that I have not made a mistake, I did:
`console.log(1 == 1)`
and that returned True
The instructions tell me that === means equal to.
Are there any problems with using == instead of ===? And, which is better to use and why?
Thanks for any help you can give me!
Using == compares only the values, === compares the type of the variable also.
1 == 1 -> true
1 == "1" -> true
1 === 1 -> true
1 === "1" -> false, because 1 is an integer and "1" is a string.
You need === if you have to determine if a function returns 0 or false, as 0 == false is true but 0 === false is false.
It really depends on the situation. It's usually recommended to use === because in most cases that's the right choice.
== means Similar while
=== means Equal. Meaning it takes object type in consideration.
Example
'1' == 1 is true
1 == 1 is true
'1' === 1 is false
1 === 1 is true
When using == it doesn't matter if 1 is a Number or a String.
http://www.w3schools.com/js/js_comparisons.asp
== is equal to || x==8 equals false
=== is exactly equal to (value and type) || x==="5" false
meaning that 5==="5" false; and 5===5 true
After all, it depends on which type of comparison you want.

Categories