JavaScript String concatenation behavior with null or undefined values

后端 未结 6 438
无人及你
无人及你 2020-12-02 17:54

As you may know, in JavaScript \'\' + null = \"null\" and \'\' + undefined = \"undefined\" (in most browsers I can test: Firefox, Chrome and IE). I

6条回答
  •  暖寄归人
    2020-12-02 18:30

    To add null and '' they need to meet a minimum common type criterium which in this case is a string type.

    null is converted to "null" for this reason and as they are string the two are concatenated.

    The same happens with numbers:

    4 + '' = '4'

    as there is a string in there which can't be converted to any number, so the 4 will be converted to string instead.

提交回复
热议问题