MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/21ezh3/javascript_equality_table/cgcqg77/?context=3
r/programming • u/vz0 • Mar 26 '14
331 comments sorted by
View all comments
0
So I'm currently doing this in a lot of places:
if (obj.x !== undefined && obj.x) { // do something }
because
if (obj.x) { //do something }
throws an error if obj.x is undefined.
Anyone know of a better way?
4 u/fjonk Mar 26 '14 obj.undefinedProperty returns undefined when undefinedProperty is not assigned a value(or if it is assigned to undefined). So the code below is valid and should not throw an exception.if ({}.x) { // will not run } else { // will run }http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.9
4
obj.undefinedProperty returns undefined when undefinedProperty is not assigned a value(or if it is assigned to undefined). So the code below is valid and should not throw an exception.
undefined
if ({}.x) { // will not run } else { // will run }
http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.9
0
u/optymizer Mar 26 '14
So I'm currently doing this in a lot of places:
because
throws an error if obj.x is undefined.
Anyone know of a better way?