return String vs Integer vs undefined vs null

后端 未结 6 1748
醉话见心
醉话见心 2021-02-15 10:50

Why does javascript prefers to return a String over any other choices ?

Consider the following snippet.

var arr = [\'Hello1\', \'Hello2\', \         


        
相关标签:
6条回答
  • 2021-02-15 10:57

    The code a || b is roughly equivalent to a ? a : b, or this slightly more verbose code:

    if (a) {
        result = a;
    } else {
        result = b;
    }
    

    Since || is left-associative the expression a || b || c is evaluated as (a || b) || c.


    So in simple terms this means that the || operator when chained returns the first operand that is "truthy", or else the last element.

    This feature can be useful for providing defaults when you have missing values:

    var result = f() || g() || defaultValue;
    

    You could read this as: Get the result of f(). If it's a falsey value, then try g(). If that also gives a falsey value, then use defaultValue.

    0 讨论(0)
  • 2021-02-15 11:00

    The statement

    return this[x] || null || 'aïe' || 12 || undefined ;
    

    will evaluate the subexpressions left to right, and will return the first subexpression that does not evaluate to false. The non-existent element evaluates to false as it's undefined, and null is false by definition. That leaves the string as the first non-false subexpression, so that's what you get.

    0 讨论(0)
  • 2021-02-15 11:05

    The string is the first truthy value in the chain of or's. Simples.

    0 讨论(0)
  • 2021-02-15 11:11

    return this[x] || null || 'aïe' || 12 || undefined will not return one of those. It is supposed to return the result of the expression this[x] || null || 'aïe' || 12 || undefined - I believe it will return a boolean value.

    0 讨论(0)
  • 2021-02-15 11:12

    Because it is evaluating in order from left to right.

    If you were to modify to this:

    return this[x] || null || 12 || 'aïe' || undefined ;
    

    Your answer would be 12.

    0 讨论(0)
  • 2021-02-15 11:18

    Because this[x] is undefined, which is falsy, and so is null.

    The || operator returns the first "truthy" value it finds, and stops its evaluation at that point.

    If no "truthy" value is found, it returns the result of the last operand evaluated.

    There are a total of 6 "falsey" values. They are...

    1. false
    2. undefined
    3. null
    4. ""
    5. NaN
    6. 0

    Everything else is considered truthy.

    So your expression will be evaluated as...

    //   v--falsey            v--truthy! return it!
    ((((this[x] || null) || 'aïe') || 12) || undefined);
    //               ^--falsey         ^--------^---these are not evaluated at all
    

    Or you could look at it like this:

    (
      (
        (
          (this[x] || null) // return null
                /* (null */ || 'aïe') // return 'aïe' and stop evaluating
                                    || 12) 
                                          || undefined);
    
    0 讨论(0)
提交回复
热议问题