Blog Post

When 2 + 2 is actually 22

A common place where JavaScript’s internal typing system happens is when you try to add a number (2) and a string that looks like a number (“2”). In JavaScript, 2 + “2” is “22”. Since one of these is a string, it casts/treats the other as a string too and puts them together or concatenates them. This can be very useful when you need to construct a message for the user, but can cause weird errors if you don’t what type a variable holds.