最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why is number + string a string in javascript? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Oct 29, 2014 at 22:05 ManirajSS 2,3755 gold badges29 silver badges51 bronze badges asked Sep 25, 2014 at 11:54 Robin-HoodieRobin-Hoodie 4,9744 gold badges32 silver badges64 bronze badges 7
  • 6 An addition (even of different types) that follows the commutative law isn't a bad thing in general ;-) – linqu Commented Sep 25, 2014 at 12:00
  • 2 possible duplicate: stackoverflow.com/questions/5961000/… (first one found, I'm sure there are many out there) – rlemon Commented Sep 25, 2014 at 12:07
  • 2 @linqu Expect that it is not commutative because "5" + 1 == "51" != "15" == 1 + "5". – Bakuriu Commented Sep 25, 2014 at 16:28
  • It makes more sense than some other uses of the + operator in JavaScript. – rickster Commented Sep 26, 2014 at 5:29
  • That's indeed some weird behaviour – Robin-Hoodie Commented Sep 26, 2014 at 6:33
 |  Show 2 more comments

5 Answers 5

Reset to default 27

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.

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.

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.

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

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.

发布评论

评论列表(0)

  1. 暂无评论