Aditya Thakur
Aditya Thakur

Reputation: 2610

Need some explaination regarding why "1"- -"1" expression returns 2?

Going through an online test, i found this weird expression where

"1"- -"1" = 2

i understand -"1" will be converted to number, but the other "1"- is getting converted too?

Upvotes: 2

Views: 89

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44087

You can't subtract strings - so they are converted to numbers:

1 - -1

Two minuses make a plus:

1 + 1

And if you can prove it, that is equal to 2:

1 + 1 = 2

console.log("1" - - "1");

Upvotes: 6

Related Questions