8

In case of operation

1 + '1',

the number 1 is converted to string and appended to the later string then why isn't it the case for

1 * '1'

Pawan
  • 605
  • 1
  • 6
  • 18
  • 8
    Multiplication operator: http://www.ecma-international.org/ecma-262/5.1/#sec-11.5 Addition operator: http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.1 – dfsq Jul 31 '14 at 10:18
  • 5
    It doesn't make sense to multiply strings, so the operands are converted to numbers. But it does make sense to concatenate strings, so operands are converted to strings (it's arbitrary, but documented behavior). – pawel Jul 31 '14 at 10:19
  • 1
    @pawel Actually string multiplication *could* make sense, there could be something like `3 * 'a' == 'aaa'` for instance. – Lucas Trzesniewski Jul 31 '14 at 10:49
  • @LucasTrzesniewski you're right, it *could* work this way and it would even be fun to have another ambigous operator :) But i thought about `'a' * 'b'` which would be the case if type conversion in multiplication was analogous to addition. – pawel Jul 31 '14 at 11:09

5 Answers5

15

Because + is overloaded.

+ can mean either addition or string concatenation. In the former case, JavaScript attempts to do string concatenation rather than addition, so it converts everything to a string and carries out the string concatenation. In the latter case, the only option is to multiply, so it converts everything to something that can be multiplied and carries out the multiplication.

dfsq linked the specification of addition syntax in the comments under your question, which explains why JS attempts string concatenation instead of addition: It checks whether the things you're adding together are strings, and then if at least one of them is, attempts string concatenation - and otherwise, attempts addition.

Community
  • 1
  • 1
Matthew
  • 2,232
  • 4
  • 23
  • 37
  • 5
    `JavaScript has decided` is not a real explanation... – Andreas Jul 31 '14 at 10:19
  • 1
    It's a simple explanation. The specification of how JavaScript handles binary operators is probably beyond the scope of this question, in my opinion - the important bit is that it's an overloaded operation. (I'm happy to change it if you have a similarly simple, yet real, explanation, though) – Matthew Jul 31 '14 at 10:21
  • 10 up votes? who is up voting this? @Matthew JavaScript cannot decide, contributors can. – Kyle Needham Jul 31 '14 at 10:24
  • Fair enough, I guess. I'll come up with something better to say! :D – Matthew Jul 31 '14 at 10:24
  • 1
    Is that a better answer? I reworded the questionable components and attempted to work in an explanation of *why* string concatenation is prioritised. – Matthew Jul 31 '14 at 10:28
7

The + is a concatenation operator for strings. As a result, the number gets converted to a string and then concatenated. The concatenation takes preference over numeric addition. If you want to make it add them instead, use parseInt, like 1 + parseInt('1')

The * is not a valid operator for strings at all, so it converts the string to a number and then does the operation.

This is a simple case, so the order of operands don't matter. If you get more complex, it tends to get even more interesting. For instance:

1 + 1 + '1' = '21'
'1' + 1 + 1 = '111'

For more information, check out this MDN article on the matter

neelsg
  • 4,802
  • 5
  • 34
  • 58
2

+ is used for string concatenation * is used for multiplicatio

In 1 + '1' '+' will concatenate 1 with '1'

You need to do following

1 + parseInt('1')
sumit
  • 15,003
  • 12
  • 69
  • 110
0

In javascript + indicates concatination. That's why when you try to add a number(i.e. 1) to a string ('1'),it becomes 11. And it treats * as multipication, so it multiplies a number (1) with a string ('1') and gives result as 1. e.g. (1*a= a).

  • 1
    `+` also indicates addition - not just concatenation. – Matthew Jul 31 '14 at 10:20
  • 1
    Yes when it is written between two numbers it is considered as addition. But the question is about a number and a string.So in this case it is performing concatenation. – Snehal Nagdeote Jul 31 '14 at 10:27
0

"+" Operator is used for both string concatenation and normal mathematical addition so when we use this operator between a number and string it will just concatenate those two. But "*" Operator not like that it will only perform multiplication if this used between a number and a pure string it wont give proper output.But, if it is used between a number and again a number in string format it will consider both as number and give the multiplication of those two.