0

While digging through JavaScript's data types, I learnt that:

Strings cannot be divided, multiplied, or subtracted, but the + operator can be used on them. However, there are no such restrictions for numbers.

This is a bit self-explanatory. But, I'm still not clear with few questions like:

  1. How does JavaScript implement the arithmetic operations on strings in JavaScript?
  2. How is it different for different data types?
  3. Or is it true, that arithmetic operations are allowed/meant only and only for numbers. For all other data types, it's just another way of representing other operations (like string/array concatenation)?
Aaditya Sharma
  • 3,330
  • 3
  • 24
  • 36
  • `+` on strings is concatenation. Whether or not you want to call this arithmetical is a matter of semantics. Other than that, this question is too broad. – John Coleman May 11 '19 at 17:32
  • Agreed on the point that the question could seem to be broad. I'd appreciate if you could suggest some edit? – Aaditya Sharma May 11 '19 at 17:37

1 Answers1

0
  1. There are no arithmetic operators for strings, there is only the + operator, and that is string concatenation. "Hello " + "World" == "Hello World"
  2. Arithmetic operations are defined for numbers and booleans, though true and false may get cast to 1 and 0.
  3. Operations with other types might either throw a SyntaxError or convert them into a string, NaN or 0. I am not too sure which when happens.
Fox Sleigh
  • 327
  • 4
  • 8