3

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 is not null and that it must have valid id property.

var obj = { id: 111 };
function test() {
    return (obj && obj.id);
}

I am expecting that this function will always return boolean but in fact it returns undefined if the obj is undefined or value of obj.id if object exists like in case above. Why this function return 111 instead of true.

I am going to rip off hair of my head ... Please illuminate my mind :)

3
  • 1
    What this code says is "if obj exists, return obj.id". You could do obj && obj.id !== null Commented Dec 3, 2018 at 15:52
  • What you want is to add !! which will force it to convert to boolean. return !!(obj && obj.id) Commented Dec 3, 2018 at 15:52
  • @Oram Welcome to JavaScript world :D Commented Dec 3, 2018 at 15:53

2 Answers 2

4

It's a common misconception. In JS (unlike in e.g. PHP) an expression like x && y does this:

  1. execute the expression x
  2. if the expression x returned true, then execute the expression y as well and return it (y). Otherwise return x (which would be falsy in this case e.g. 0, '', false, null, undefined).

In other words it works more like a ternary expression x ? y : z.

If you want a boolean, then use !!(x && y).

Sign up to request clarification or add additional context in comments.

7 Comments

Good explanation, but what is z in the ternary expression?
@YeldarKurmangaliyev Seriously? ) I've added the ternary expression as an example to demonstrate that binary expression x && y is a conditional logical expression just like a ternary expression. z is an alternative case of ternary logical expression.
I think @YeldarKurmangaliyev was asking a simple (and good) question. Perhaps a more apt example would be x ? y : x, as the && operator will return x if it is falsy.
@TylerRoper The "Seriously" was just my reaction after I saw his rating and impressive JS experience. He is also is the first person whom I saw here from my country BTW
@NurbolAlpysbayev Understood :) Simply a hiccup in communication!
|
0

When obj is defined, why does if (obj && obj.id) return 111?

The logical AND (&&) operator returns expr1 if it can be converted to false; otherwise, returns expr2.

MDN: Logical Operators - Description (slightly paraphrased)

expr1 (obj) cannot be converted to false, therefore it returns expr2 (111).


Why does it not return true?

Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the && and || 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.

MDN: Logical Operators

Because you are using the logical operator with non-Boolean values, the result will be non-Boolean as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.