Why am I getting this result in Javascript?

Question:

I've seen a question recently and I don't understand why the result is giving me a numeric value if it's string operations.

The operation is "1" - - "1" , and the logic would tell me that the result is "11" , but it is 2 .

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

What is the cause of this behavior?

Answer:

To concatenate Strings in JS use +, so "1" + "1" return "11"

You cannot perform subtraction (subtraction) on strings, so JS converts both "1" to numbers and performs the operation, resulting in 1--1 = 1+1 = 2

Scroll to Top