1

I had a strange bug that I just found the source of in my code.

"1" + .88 // 10.88

What's going on here?

1
  • 3
    More accurately, it's "10.88", not 10.88. Commented Jan 4, 2016 at 14:40

1 Answer 1

8

When adding a number and a string (whatever their order), the number is converted to a string and then the two are concatenated.

.88.toString()

is

"0.88"

So you get the string

"10.88"

which appears as

10.88

in most contexts (for example in an HTML input).


If you want a specification based analysis, it starts here with

enter image description here

Then the number to string conversion with the leading 0.is described here: enter image description here (s=88, k=2, n=0)

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

3 Comments

but why 1 is converted to 10 in his case?
@JurijJazdanov It's not, the 0 is coming from .88.
@NinaScholz It's the same for the addition: when one of the operands is a string, the other is converted to a string, whatever the order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.