Sai Pardhu
Sai Pardhu

Reputation: 289

Why is "1" + + "1" = "11" but "1" - - "1" = 2 in JavaScript

Today, I was made aware of an intriguing thing by my co-worker. Basically, he opened the console(Ctrl+shift+I) in the google chrome and typed the following:

"1" + + "1" 

(please be aware of the spaces between the operators) This gave the result as "11" which is string and I totally understand it.

However, when the input is

"1" - - "1"  

the console answered 2.

Similarly I tried

"1" - - "4" 

and console outputs 5.

Also,

"1" - + "4"

gives the output -3.

I understand the logic but have no clue about the working of these operators.

Any explanation would be appreciated.

Thanks in advance.

Upvotes: 1

Views: 759

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190905

the - operator has no meanings on strings, so it converts the value to a Number.

Upvotes: 5

Related Questions