Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

A problem with boolean expression evaluation in 1.4 #11959

Closed
SamuraiPrinciple opened this issue May 28, 2015 · 7 comments
Closed

A problem with boolean expression evaluation in 1.4 #11959

SamuraiPrinciple opened this issue May 28, 2015 · 7 comments

Comments

@SamuraiPrinciple
Copy link

Greetings,

After upgrading to angular 1.4 we're having a problem with expressions like:

true && aMissingObject.aMissingProperty

They evaluate to true, instead of false (incidentally, true && !!aMissingObject.aMissingProperty evaluates to false).

Here is an example on plunkr:

http://plnkr.co/edit/4wVwHOO3lsc10JaSrIXI

Could you please comment if this is expected behaviour?

Many thanks

@Narretz
Copy link
Contributor

Narretz commented May 28, 2015

Thanks for the report. This happens since 1.4.0-beta.3, so it looks like it's caused by the parse refactor. @lgalfaso can you take a look?

@Narretz Narretz added this to the 1.4.1 milestone May 28, 2015
@Narretz Narretz changed the title A problem with ng-if (or rather expression evaluation) in 1.4 A problem with boolean expression evaluation in 1.4 May 29, 2015
lgalfaso added a commit to lgalfaso/angular.js that referenced this issue May 31, 2015
When there is an expression of the form `a.b` and there is a recursion that
specifies where the value should be placed, then when `a == null` then set
the value of the returned identifier to `undefined`

Closes angular#11959
@lgalfaso
Copy link
Contributor

lgalfaso commented Jun 1, 2015

@SamuraiPrinciple thanks for reporting this! The fix landed and should be part of the next release

@SamuraiPrinciple
Copy link
Author

Indeed - just tried it on master and it works like a charm.

Much appreciated

@christianvuerings
Copy link
Contributor

@lgalfaso any idea on when this might land?

@lgalfaso
Copy link
Contributor

This already landed and should be part of 1.4.1 once released

raydavis pushed a commit to raydavis/calcentral that referenced this issue Jun 10, 2015
samccone added a commit to samccone/todomvc that referenced this issue Jun 28, 2015
samccone added a commit to samccone/todomvc that referenced this issue Jun 28, 2015
netman92 pushed a commit to netman92/angular.js that referenced this issue Aug 8, 2015
When there is an expression of the form `true && a.b` and where `a == null`, then set
the value of `true && a.b` to `undefined`.

Closes angular#11959
bhollan pushed a commit to bhollan/todomvc that referenced this issue Oct 5, 2015
@jziggas
Copy link

jziggas commented Jan 30, 2019

Commenting on here as I can't seem to find anything related, I just ran into an issue similar to this. I had a line of code this.canEdit = this.item && this.item.id where I would normally expect the value to short-circuit to false when this.item is undefined, however it evaluated this.canEdit to undefined!

I had to rewrite the line to this.canEdit = this.item !== undefined && this.item.id !== undefined for it to evaluate to a boolean value. Weird?

This is on Angular @ 7.1

@codymikol
Copy link

codymikol commented Jan 31, 2019

@jziggas This is actually the github for angularJS 1.x, but what you're doing is essentially evaluating

this.canEdit = undefined && 'Not Undefined'

Which does return undefined, so it seems to be working as expected.

image

You can use !! and parens to convert that statement into a bool like this

image

So essentially

this.canEdit = !!(this.item && this.item.id)

When either of these is falsey, it will return false

If you're wondering why does this happen, this is because logical and "&&" evaluates whether left of operator (this.item) can be evaluated to truthy. If so, it returns right of operator, otherwise returns left of operator. In your case this is undefined.

You can see the same if you did something like the following

image

two is returned because one is truthy.

And I realize this is getting quite long, but for the sake of completeness and a full answer I will explain the !!

With Parens you can evaluate the result of (this.item && this.item.id) which will be undefined.

So now you have

!!undefined

The ! operator returns false if its operand "undefined" is truthy otherwise true.

undefined is not truthy so now you have

!true

and the final result is obviously

false

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants