Loading ...
Sorry, an error occurred while loading the content.

Re: [JavaScript] Re: recursive routines (functions)

Expand Messages
  • liorean
    ... Not really true. null+true // = 1 false+undefined // = NaN Infinity+function(){} // = Infinityfunction () { u000a} In essence what happens is that if
    Message 1 of 5 , Nov 30, 2006
    • 0 Attachment
      On 30/11/06, Jon Stephens <jon@...> wrote:
      > This is not the case.
      >
      > It's not a matter of "context", it's a matter of the types of the
      > operands as determined at the time that the expression is evaluated. If
      > at least one of the operands is a string, then JavaScript performs
      > concatenation:
      >
      > 2 + "3" -> "23"
      > "2" + 3 -> "23"
      > 2 + 3 -> 5
      >
      > IOW, only if both operands are numbers is the result a number.

      Not really true.
      null+true // => 1
      false+undefined // => NaN
      Infinity+function(){} // => 'Infinityfunction () {\u000a}'

      In essence what happens is that if any of the operands is a compound
      type (object, array, function or other reference type) or a string,
      both are converted to strings and then concatenated. Essentially
      equivalent to what this code would do:

      String(leftoperand).concat(String(rightoperand))

      If none of the operands are compound types or strings, both are
      converted to floating point numbers and added together. These floating
      point numbers may be NaN or Infinity, however. Essentially equivalent
      to what this code would do:

      Number(leftoperand)+Number(rightoperand)
      --
      David "liorean" Andersson
      <uri:http://liorean.web-graphics.com/>
    • Jon Stephens
      ... You re correct, of course, and I m aware of what you re talking about. However, I was keeping things simple and restricting my discussion to numbers and
      Message 2 of 5 , Dec 1, 2006
      • 0 Attachment
        > Re: recursive routines (functions)
        > Posted by: "liorean" liorean@... liorean
        > Date: Thu Nov 30, 2006 10:38 am ((PST))
        >
        > On 30/11/06, Jon Stephens <jon@...> wrote:
        >> > This is not the case.
        >> >
        >> > It's not a matter of "context", it's a matter of the types of the
        >> > operands as determined at the time that the expression is evaluated. If
        >> > at least one of the operands is a string, then JavaScript performs
        >> > concatenation:
        >> >
        >> > 2 + "3" -> "23"
        >> > "2" + 3 -> "23"
        >> > 2 + 3 -> 5
        >> >
        >> > IOW, only if both operands are numbers is the result a number.
        >
        > Not really true.
        > null+true // => 1
        > false+undefined // => NaN
        > Infinity+function(){} // => 'Infinityfunction () {\u000a}'

        You're correct, of course, and I'm aware of what you're talking about.
        However, I was keeping things simple and restricting my discussion to
        numbers and strings only, since those were the only types involved in
        this particular case. I felt it unnecessary to discuss any others in
        this case.

        I'm sorry if that wasn't clear to you: perhaps I should have said "when
        speaking of operands which are numbers and/or strings [exclusive of any
        other types], then the result is a number only if both operands are
        numbers".

        My point was that the post to which I was replying was wrong about JS
        not knowing the types of the operands - which it does - and its omission
        of the fact that [string] + [number] always implies concatenation and a
        string result.

        cheers

        j.

        > In essence what happens is that if any of the operands is a compound
        > type (object, array, function or other reference type) or a string,
        > both are converted to strings and then concatenated. Essentially
        > equivalent to what this code would do:
        >
        > String(leftoperand).concat(String(rightoperand))
        >
        > If none of the operands are compound types or strings, both are
        > converted to floating point numbers and added together. These floating
        > point numbers may be NaN or Infinity, however. Essentially equivalent
        > to what this code would do:
        >
        > Number(leftoperand)+Number(rightoperand)




        --
        This message has not been scanned for viruses.

        Since I do not use a Microsoft operating
        system or software, and use only plaintext
        for email, there is little need for me to do so.
      Your message has been successfully submitted and would be delivered to recipients shortly.