Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is number + string a string in javascript?

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?

like image 339
Robin-Hoodie Avatar asked Sep 25 '14 11:09

Robin-Hoodie


People also ask

Can a number be a string in JavaScript?

JavaScript Number toString()The toString() returns a number as a string.

Is a number a string?

A string consists of one or more characters, which can include letters, numbers, and other types of characters. You can think of a string as plain text.

What is difference between number and string in JavaScript?

When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. An empty string converts to 0. A non-numeric string converts to NaN which is always false . When comparing two strings, "2" will be greater than "12", because (alphabetically) 1 is less than 2.

How can a string be converted to a number?

You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class. It's slightly more efficient and straightforward to call a TryParse method (for example, int.


2 Answers

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.

like image 187
dfsq Avatar answered Sep 21 '22 08:09

dfsq


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.

like image 38
Eric Avatar answered Sep 18 '22 08:09

Eric