2

I'm a newbie in Javascript, and I'm trying to understand logical operators. I'm reading this document: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_operators

For example:

var a1 = true && true;     // t && t returns true
var a2 = true && false;    // t && f returns false
var a3 = false && true;    // f && t returns false

I get this part because the variables are boolean values. I believe it is the same thing as:

var a1 =  1 && 1;    // t && t returns 1
var a2 =  1 && 0;    // t && f returns 0
var a3 =  0 && 0;    // f && t returns 0

But when strings appear, I become confused:

var a6 = false && 'Cat';    // f && t returns false
var a7 = 'Cat' && false;    // t && f returns false
var a5 = 'Cat' && 'Dog';    // t && t returns Dog

How can I compare boolean values with strings? What is the boolean value of each string?

kyle
  • 691
  • 1
  • 7
  • 17
Roger_88
  • 477
  • 8
  • 19
  • Possible duplicate of [Logical operator && and two strings in javascript](https://stackoverflow.com/questions/17200315/logical-operator-and-two-strings-in-javascript), or more generally [Why don't logical operators (&& and ||) always return a boolean result?](https://stackoverflow.com/q/5417969/4642212). – Sebastian Simon Jan 22 '19 at 01:13
  • the only thing I understood is false and anything is false. But interested to know about the and operator of two string. Even If you add 10 and operators, the string at the lost appears. And for OR operator, the first string appears. But don't know why. But the result is for any defined variables with and operator gives true except for false and null. – Prabhakaran Jan 22 '19 at 05:37

3 Answers3

2

What you're describing are truthy and falsy values. Here are all falsy values:

false
null
undefined
0
""
NaN

And MDN says here that:

All other values, including all objects, evaluate to true when passed to a conditional statement.

So anything not included in the list above is true.

So, when you're comparing these:

var a6 = false && 'Cat';    
var a7 = 'Cat' && false;    
var a5 = 'Cat' && 'Dog';

It evaluates to this:

var a6 = false && true
var a7 = true && false
var a5 = true && true

Which evaluate to:

var a6 = false
var a7 = false
var a5 = true

Demonstrations:

Using true and false:

var a1 =  true && true;     
var a2 =  true && false;    
var a3 = false && true;     

console.log(a1);
console.log(a2);
console.log(a3);

Using 1 and 0:

var a1 =  1 && 1;     
var a2 =  1 && 0;    
var a3 = 0 && 0;     

console.log(a1);
console.log(a2);
console.log(a3);

Using false and 'Cat'/'Dog':

var a1 =  false && 'Cat';     
var a2 =  'Cat' && false;    
var a3 = 'Cat' && 'Dog';     

console.log(a1);
console.log(a2);
console.log(a3);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • *"So, when you're comparing these: `var a5 = 'Cat' && 'Dog';` It evaluates to this: `var a5 = true && true` Which evaluate to: `var a5 = true`"* No, `'Cat' && 'Dog'` is `"Dog"`, as shown in your last snippet. The result of `&&` isn't coerced. Likewise, `false && ''` is `false`, but `'' && false` is `""`. – AuxTaco Jan 22 '19 at 03:47
  • Sorry @AuxTaco, I don't have much experience with truthy/falsy. Feel free to improve my answer as you see fit. – Jack Bashford Jan 22 '19 at 04:15
1

From logical operators documentation for operator && in the syntax expr1 && expr2:

If expr1 can be converted to true, returns expr2; else, returns expr1.

In javascript, all non-empty strings are converted to True.

spaniard
  • 541
  • 2
  • 12
0

On digging a bit:

&&: The first falsy value gets returned, if there is none, the last truthy value is being returned.

var a1 = true && 'Cat' // no false values so returns true
var a2 = 'cat' && false // one false is there so returns false
var a3 = 'cat' && undefined // returns false
var a4 = 'cat' && null // returns false
var a5 = 'cat' && 1 // returns true
var a6 = 'false' && 'false' // returns true

and so on....

In a similar way ||: The first truthy value gets returned, if there is none, the operation will equal to the last falsy value.

Hope this helps you.

Prabhakaran
  • 1,524
  • 2
  • 13
  • 21