Linked Questions
38 questions linked to/from Why don't logical operators (&& and ||) always return a boolean result?
0 votes
7 answers
2k views
Javascript && operator - clarification? [duplicate]
edit : Quote removed. is not related to the question. Question How come does it always (when all are truthy) takes the last value ? Examples : f= 1 && 2 && 3 //3 f= 'k' &&...
1 vote
6 answers
2k views
1&&2&&3 returns 3. Can someone explain why [duplicate]
I got asked this question recently in an interview. I know that in JavaScript evaluation goes left to right. 1&&2 should be false right? I read in another ask here that 1&&2 returns 2....
1 vote
1 answer
2k views
Returning && expression from JS function [duplicate]
Here is the function I am working with. I actually found it in React Native documentation : var testFunction = function(word) { return word && '🍕'; } Here is how I am using this function : ...
1 vote
2 answers
3k views
why the value of console.log(1 && 2) is 2 instead of true or 1 in javascript? [duplicate]
I had checked for many test cases. It's looking if the first value is true and it's returning the second value. Example: 2 && 5 // --> 5 5 && 10 // --> 10 0 && 2 // --&...
2 votes
1 answer
746 views
Why in Javascript is it possible for a boolean operator to return neither false nor true? [duplicate]
var operand1 = null; var operand2 = true; var booleanOperatorReturnsABoolean = operand1 && operand2; booleanOperatorReturnsABoolean == false || booleanOperatorReturnsABoolean == true Result: ...
4 votes
2 answers
2k views
Why does function sometimes return 0 and sometimes return false? [duplicate]
I wrote the following Javascript function that is meant to return true when exactly one of the arguments is truthy: function onlyOne( a, b, c) { return (a && !b && !c) || (...
0 votes
2 answers
2k views
if a=2 and b=3 then how a && b = 3 rather than true? [duplicate]
There is one interview question below. The logical AND of two truths should be true. But the output is 3. Why? var a = 2; var b = 3; var c = a && b; // value of c = 3 console.log(...
0 votes
3 answers
97 views
javascript || operator gives different results based on order [duplicate]
I understand a||b returns an object, not boolean value. I just can't figure out why javascript gives different results for undefined || "" (result is "") "" || undefined (result is undefined) which I ...
1 vote
2 answers
580 views
Why does this function return 0 [duplicate]
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....
3 votes
2 answers
184 views
JS object null checking - weird JS problem [duplicate]
Imagine this simple scenario. I have variable that can be plain JS object with one property, ID, that is a number or obj variable can be null. I have simple test() function that checks if the variable ...
2 votes
1 answer
211 views
Double-negation + logical and in Javascript [duplicate]
I just ran across this code var someBool = !!(o.a && o.a.t); I was about to remove the double-negation, then realized it forces someBool to be a boolean value... Looking on the MDN, I find ...
1 vote
2 answers
423 views
This condition returns a boolean when checking for a falsy, but it returns a string when checking for a truthy value. How can I get it to work? [duplicate]
I get a boolean return when checking for a falsy value, but not a boolean when checking for a truthy value... I am just getting a string instead. This works. const isFalse = !values.firstName &&...
1 vote
1 answer
58 views
How does using logic operators populate a var? [duplicate]
Let's take a look at this code: var token = (req.body && req.body.access_token) || (req.query && req.query.access_token) || req.headers['x-access-token']; ...
0 votes
1 answer
69 views
javascript: expecting true or false but getting number [duplicate]
I have something like the following: var val = "string"; var testVal = val && val.length; I would expect testVal to be either true or false but it is the length of the string. Not sure why ...
0 votes
0 answers
60 views
JavaScript - OR Operation with two false values [duplicate]
Related to coerce and default values in JavaScript, given this code: function greet(name){ name = name || ''; console.log("Hello " + name); } greet(); Not given any name as parameter, we have ...