Search code examples
javascriptlogic

im in confusion about this logic, can someone explain


let Output = "1" - + -1;
console.log(Output);

This code above will return 2, but im confused how the logic works. Can someone Explained it or break it down for me.

P.S: its String and Integer(Number)


Solution

  • The subtraction operator forces both sides of the operation to be numbers, so the string on the left is converted to one.

    The unary plus operator converts the right hand side to a number, but negative one is already a number so it does nothing.

    One minus negative one is two.