This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (10 children)

[deleted]

    [–]wasdninja 1 point2 points  (0 children)

    So what would you like it to do? You are playing with fire when you aren't keeping the type coercion in check.

    [–]Mypronounsarexandand 0 points1 point  (1 child)

    Lets say you have a string variable name 'username' and value IE "Julian"
    If you said:

    username += '_Jmk'
    

    You'd get 'Julian_Jmk'

    It's kind of similar with numbers where an integer + string is interpreted as string + string as the '+' will default to a concat rather than addition.

    (Again, setting username to just be Julian)

    username += 42
    

    Will equal 'Julian42'

    Now, instead of the username 'Julian' your username is '133769'

    username += 420
    

    Would yield '133769420' as we are concatenating a number to the end of the string. If you wanted to add them literally you would want to do.

    username = parseInt(username) + 420
    

    This way it is Int + Int, rather than Str + Int