-3

"123" is a string type but the value is an integer. So JavaScript convert it to an integer on compile time and allowed multiplication,Addition,etc. But "1-2" is a string type but why it does not allow to multiplication? And why JavaScript does not handle this?

console.log("1" * "123")//123
console.log(1 * "123")//123
console.log("1" * 123)//123

console.log("ABC" * 123)//NaN
console.log("ABC" * "ABC")//NaN

console.log("0" * "1-2")//NAN
console.log("0" * "1*2")//NAN
console.log("0" * "1+2")//NAN
console.log("0" * "1/2")//NAN
console.log("0" * (1-2))//0

EDIT:

  • -is not subtraction but a hyphen. OK. But it should be handle as hyphen while concatenation not calculation.

Why JavaScript can't subtract/Add "1-1" while compiling. But JavaScript convert "123" as a integer on compile time, if the code is doing calculation. Then why it's doesn't calculate "1-1" or "1+1" to 0 or 2 and convert into an integer?

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • 3
    This is because in "1-2" is taken the - is not "substraction" but a hyphen '-' character. – Exception_al Feb 22 '18 at 13:01
  • 1
    You need to read about coercion - [What exactly is Type Coercion in Javascript?](https://stackoverflow.com/questions/19915688/what-exactly-is-type-coercion-in-javascript) – Ele Feb 22 '18 at 13:01
  • 1
    https://meta.stackoverflow.com/a/261593/448144 – Nope Feb 22 '18 at 13:06
  • @RameshRajendran Maybe it should, it's just that it doesn't. See chŝdk's Answer. – Exception_al Feb 22 '18 at 13:30
  • @Exception_al I don't know why 3 down votes for my question. Is it a bad question? – Ramesh Rajendran Feb 22 '18 at 13:32
  • I didn't downvote ... I liked the question, even did some `console.log` to verify ! There you go -- an UpVote to have caught my wonder ! – Exception_al Feb 22 '18 at 13:34
  • I think that it is a design choice. It is just how it works. – Giannis Mp Feb 22 '18 at 13:36
  • `"1-2"` is a string, `1-2` is an expression, i.e. code. JavaScript converts strings to numbers in numeric context (when the string is one operand of `*`, `-`, `/` etc) but it doesn't attempt to evaluate the expressions embedded in the string. There is a special function for this purpose, assuming the string contains code (and you know what you're doing). – axiac Feb 22 '18 at 13:36
  • @axiac then what is `"123"` this is also a string right? But how it's doing calculation in JavaScript? – Ramesh Rajendran Feb 22 '18 at 13:41
  • Have you read my previous comment? Read it again! Then read this fragment of documentation: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Strings#Numbers_vs._strings – axiac Feb 22 '18 at 13:44
  • I would greatly appreciate if JavaScript correctly parsed string "A perfect website code" and inferred all the rest. – bipll Feb 22 '18 at 15:11

2 Answers2

2

Well to evaluate these strings as Number you can use eval() function, but it's not very recommended to use it. So eval("1-2"); will give you -1.


But in javascript when you use an operator, it will try to evaluate both operands to numbers before doing the operation. So in all cases the two parts of the operation will be evaluated to numbers.

Edit:

Actually in an operation JavaScript, parses only strings that can be parsed as Number, "1-2", can't be parsed as a Number. To check that try to execute Number("1-2"); you will get NaN.

So when you evaluate:

  • "123" you will get 123.
  • "ABC" you will get NaN.
  • "(1-2)" you will get NaN.
  • ("1-2") you will get NaN.

And in ("1-2") the string "1-2" won't be evaluated to 1-2 because - in a string won't be considered as an operator.

So conclusion is when you you have a string that can't be parsed/evaluated as a number, you will get NaN.

Documentation:

For further details you can check Expressions section of MDN's Expressions and operators Reference, where it says:

Arithmetic: evaluates to a number, for example 3.14159. (Generally uses arithmetic operators.)

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
1

Because Javascript is treating the "2-1" as a String. When trying to do arithmetic Javascript will attempt to convert the String into an Int. Since "2-1" is recognized as a whole string it won't reduce to just one which is why you see the NaN.

Mauro Doza
  • 331
  • 2
  • 14