0

How am I getting 339 as output of the following code ?

console.log(3 + '4' + 5 - 6)

can anyone please explain me?

1

1 Answer 1

2

3 + "4" yields "34" due to the implicit string conversion. Implicit string conversion seems to have higher priority than implicit number conversion.

"34" + 5 yields "345"

"345" - 6 yields 339 due to the implicit number conversion

Sign up to request clarification or add additional context in comments.

3 Comments

"Implicit string conversion seems to have higher priority than implicit number conversion." no, it's just processed left-to-right and that's the first operation: 1 + 2 + "3" is "33" because the first operation is 1 + 2 and then it's 3 + "3". There is no priority of conversions.
If there's no string in play, there's no need for conversion.
You said that there was some priority to the conversion. There isn't. It's just processing the operations in sequence. And a <number> + <string> will always convert to a string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.