Why is number + string a string in javascript?

雨燕双飞 提交于 2019-12-06 17:36:58

问题


Sort of trying out some quirks in javascript:

First I did

console.log("5" + 1);

This prints 51, this is normal right, both number and string have a + operator, but since string is the first variable it will convert 1 to a string.

Now when I did this:

console.log(1 + "5")

I expected output to be 6, as I thought it would convert string to a number. However, the magic output was 15.

Could anyone more experienced in javascript brighten this up for me?


回答1:


Quoting ECMAScript spec The Addition operator ( + ) section:

  1. If Type(lprim) is String or Type(rprim) is String, then
    Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)

So the order doesn't matter here.




回答2:


console.log(1 + "5") I expected output to be 6, as I thought it would convert string to a number. ...

But then what would you expect if you had written the following?

console.log(1 + " fine day")

or

console.log(1 + " answer(s) to my question")

There can be no assurance as a general rule that a string is convertible to a number. But any number can be converted to a string. That's why the conversion rules are written to move toward a type that is compatible. (In contexts where you as a programmer know that a string can be safely converted to a number, then you can do so explicitly so that the + operation is between two numbers. But that is not true in general for strings.)

In other contexts, this is also why small ints and low precision floats would be converted to large ints or double precision floats when operating on mixed types that the latter types. You can safely convert the limited forms into the larger forms, but you cannot safely go in the other direction in general. A small int can be represented as a big int or a double, but the other direction is not generally a safe conversion.

So conversion rules for operation on mixed types are written as much as is feasible to move toward mutually compatible types that are safe common types. It is left to the programmer to write explicit conversions for the special cases where a more general type can be safely converted to a more limited type.




回答3:


In both cases the value will be converted to a string. When you add a number to a string or a string to a number, the result is a string.

Simply use parseInt(value) or toString(value) to force the conversion.




回答4:


You can try code :
console.log(1 + parseInt("5")) => 6




回答5:


Concatenation also uses + in Javascript.

var result = "String" + number + obj;
// is equivalent to
string result = "String" + number.ToString() + obj.ToString();

However thing is, in C# / .net you could do the same thing, and you will get the same result - System.Console.WriteLine("result is " + 10 + 20);.

In JavaScript (and C# for that matter) strings are immutable. They can never be changed, only replaced with other strings. You're probably aware that combined + "String" doesn't directly modify the combined variable - the operation creates a new string that is the result of concatenating the two strings together, but you must then assign that new string to the combined variable if you want it to be changed.

The argument about using mathematical operators to do string concatenation is arguably an "incorrect" argument, but there's also an argument to be made that using + to do a lot of string concatenation can be very slow.



来源:https://stackoverflow.com/questions/26037997/why-is-number-string-a-string-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!