Is using && and || to shortcut typeof == "undefined" checks stable in javascript? - Stack Over

Consider the following nested data structure:var options = {option1 = {option1a : "foo",optio

Consider the following nested data structure:

var options = {
    option1 = {
        option1a : "foo",
        option1b : "bar"
    },
    option2 = {},
}

I've got a data structure like the one above, which I'm using as a list of optional parameters for inputs to a function. Since all of this is optional inputs, it may or may not exist. Initially, I was using chains of typeof x == "undefined" statements to make sure that, for example, options, option1, and option1a are all defined. My code would end up looking like this for defining a variable:

if(typeof option != "undefined" 
  && typeof option.option1 != "undefined" 
  && typeof option.option1.option1a != "undefined){
    var x = option.option1.option1a;
} else {
    var x = "default";
};

I noticed that I can shorten this to the following:

var x = option && option.option1 && option.option1.option1a || "default";

Does && consistently return the last true or first false statement? And does || consistently return the first true statement? Logic would dictate that they could return booleans in some implementations, but I don't know enough about browser / javascript implementations to make that statement definitively. Is this a safe way to go about doing this, or is there a better way?

Consider the following nested data structure:

var options = {
    option1 = {
        option1a : "foo",
        option1b : "bar"
    },
    option2 = {},
}

I've got a data structure like the one above, which I'm using as a list of optional parameters for inputs to a function. Since all of this is optional inputs, it may or may not exist. Initially, I was using chains of typeof x == "undefined" statements to make sure that, for example, options, option1, and option1a are all defined. My code would end up looking like this for defining a variable:

if(typeof option != "undefined" 
  && typeof option.option1 != "undefined" 
  && typeof option.option1.option1a != "undefined){
    var x = option.option1.option1a;
} else {
    var x = "default";
};

I noticed that I can shorten this to the following:

var x = option && option.option1 && option.option1.option1a || "default";

Does && consistently return the last true or first false statement? And does || consistently return the first true statement? Logic would dictate that they could return booleans in some implementations, but I don't know enough about browser / javascript implementations to make that statement definitively. Is this a safe way to go about doing this, or is there a better way?

Share Improve this question asked Aug 28, 2013 at 16:02 ckerschckersch 7,6972 gold badges41 silver badges48 bronze badges 2
  • 4 yes, this is guaranteed, and part of the spec. – Dave Commented Aug 28, 2013 at 16:05
  • 4 Note that your two methods don't do quite the same thing. Your second option will guarantee that none of the items are undefined, but it is also testing that none of the items are any falsey value (false, null, "", NaN, 0 and so on). If all you're trying to do is make sure they aren't undefined, then it will work, but if 0 or false or "" are valid values for option.option1.option1a, then your second option might not be doing exactly what you want. – jfriend00 Commented Aug 28, 2013 at 16:09
Add a ment  | 

1 Answer 1

Reset to default 9

Yes that will work correctly. && returns the last true statement or first false statement, || returns the first true statement or last false statement.

var a = false || 0 // a === 0
var b = false && 0 // b === false
var c = true || 1 // c === true
var d = true && 1 // d === 1

and && is evaluated before || so

var e = false || 0 && null // e === null
var f = 1 || 2 && 3 // f === 1

Its also worth mentioning that falsy values enpass more than undefined in JS, so your 2 checks are not quite the same. A value of 0 for instance, will be considered falsy, even if it is "defined"

See the official spec here for the detailed logical specification.

Syntax

LogicalANDExpression :
BitwiseORExpression
LogicalANDExpression && BitwiseORExpression
LogicalANDExpressionNoIn :
BitwiseORExpressionNoIn
LogicalANDExpressionNoIn && BitwiseORExpressionNoIn
LogicalORExpression :
LogicalANDExpression
LogicalORExpression || LogicalANDExpression
LogicalORExpressionNoIn :
LogicalANDExpressionNoIn
LogicalORExpressionNoIn || LogicalANDExpressionNoIn

Semantics

The production LogicalANDExpression : LogicalANDExpression && BitwiseORExpression is evaluated as follows:
Let lref be the result of evaluating LogicalANDExpression.
Let lval be GetValue(lref).
If ToBoolean(lval) is false, return lval.
Let rref be the result of evaluating BitwiseORExpression.
Return GetValue(rref).
The production LogicalORExpression : LogicalORExpression || LogicalANDExpression is evaluated as follows:
Let lref be the result of evaluating LogicalORExpression.
Let lval be GetValue(lref).
If ToBoolean(lval) is true, return lval.
Let rref be the result of evaluating LogicalANDExpression.
Return GetValue(rref).
The LogicalANDExpressionNoIn and LogicalORExpressionNoIn productions are evaluated in the same manner as the LogicalANDExpression and LogicalORExpression productions except that the contained LogicalANDExpressionNoIn, BitwiseORExpressionNoIn and LogicalORExpressionNoIn are evaluated instead of the contained LogicalANDExpression, BitwiseORExpression and LogicalORExpression, respectively.
**NOTE** The value produced by a && or || operator is not necessarily of type Boolean. The value produced will always be the value of one of the two operand expressions.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744825665a4595779.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信