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

Why does JavaScript evaluate plus with string and int differently? - Stack Overflow

programmeradmin1浏览0评论

Why does JavaScript evaluate plus with a string and integers differently depending on the place of the string?

An example:

console.log("1" + 2 + 3);
console.log(2 + 5 + "8");

The first line prints 123 and the second prints 78.

Why does JavaScript evaluate plus with a string and integers differently depending on the place of the string?

An example:

console.log("1" + 2 + 3);
console.log(2 + 5 + "8");

The first line prints 123 and the second prints 78.

Share Improve this question edited Jul 15, 2015 at 20:27 nirgn asked Jul 15, 2015 at 7:25 nirgnnirgn 2,0744 gold badges24 silver badges30 bronze badges 5
  • 1 the first converts the whole thing to string. the second starts adding up the numbers until it sees the string, when it will then convert the whole thing to string. – Callum Linington Commented Jul 15, 2015 at 7:26
  • 1 Because in javascript + is both the addition and concatenation operand, and which one is applied depends on the first value. – Matteo Tassinari Commented Jul 15, 2015 at 7:26
  • Because string+string is still a string. Adding a string to an int type-safely is impossible, so it coerces the second operand to be the same type as the first. – doldt Commented Jul 15, 2015 at 7:26
  • 1 here is problems about javascript wiki.theory.org/YourLanguageSucks – tanaydin Commented Jul 15, 2015 at 7:28
  • 1 I'm just going to leave this here: xkcd.com/1537 – Luke Commented Jul 15, 2015 at 11:27
Add a comment  | 

5 Answers 5

Reset to default 21
  1. JavaScript does automatic type conversion
  2. The expression is evaluated left to right and therefore:

    "1" + 2 + 3 -> "12" + 3 -> "123"
    
    2 + 5 + "8" -> 7 + "8" -> "78"
    

JavaScript's type conversion in the cases you mention infers that you're looking to convert the types of all of your arguments to match the type of your first argument to the + operator.

This is why in the first case you mention that starts with the string "1" the addition converts the other arguments to strings.

In the second case you mentioned console.log(2 + 5 + "8"). The first argument to the + operator is an integer which is why JavaScript's type conversion assumes you want an integer.

Well, that's because the + operator is overloaded.

  • When used with two integers, it sums them up.
  • When used with two strings, it concatenates them.
  • When used between a string and an integer, it concatenates them.

That's why, when you do

console.log("1" + 2 + 3);

It concatenates the first and second operand (since the first one is a string) to give a string, "12", and it again concatenates it with the third operand for the same reason.

However, when you do

console.log(2 + 5 + "8");

It sums the first and second operands (both being numeric) to give 7 and finally concatenates it with the third operand for reasons mentioned above.

The expression evaluates from left to right.

So if you use "1" + 2 + 3 and the types of operands are different, it will append, otherwise if the types are ints it will add.

case "1" + 2 + 3
"1" +2 - different types- result - "12" string
"12" + 3 - different types- result - "123" string
 case 2 + 5 + "8"
2 + 5 - same types- result - 7 int
7 + "8" - different types - result "78" string

I think it's called duck casting. If it looks like a duck, and quacks like a duck it must be a duck. If it looks like a string "1" then it's probably a string.

JavaScript also seems to evaluate both from left to right and from specific(int) to general (string).

In your first example, console.log("1" + 2 + 3), it's evaluating the "1" as a string because of the quotes and assuming you want to concatenate everything following it. It writes "123" as the result.

In the second example, console.log(2 + 5 + "8"), it takes an int and adds to another int producing 7, then encounters a string, so it assumes you want to do a concatenation so it writes "78" as a string. If you wanted to evaluate strictly int values, you could use parseInt("8") to convert your 8 back into an int.

发布评论

评论列表(0)

  1. 暂无评论