Alixy Mizuki
Alixy Mizuki

Reputation: 91

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)

Upvotes: -1

Views: 38

Answers (1)

Quentin
Quentin

Reputation: 943615

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.

Upvotes: 4

Related Questions