“-0” does string-to-number conversion and therefore forces engine to perform addition:

(“50” - 0) + 1 = 51

However, this trick (and the same like *1 etc) is considered “bad style”.

The plus sign is problematic because it can represent addition of two numbers or ‘concatenating’ (joining) two strings. So, when javascript sees this:

“100” + 50

it doesn’t know whether it should be adding two numbers or joining two strings, so by default, when it sees a number and a string, js automatically converts 50 to a string and then joins the two strings.

In js, all values from input fields and attributes are strings–even if they look like numbers. So, what you are doing in your code in this line:

getAttribute(‘height’)+50)

is this:

“27” + 50

and by default javascript converts 50 to a string and joins the two strings.

However, there is no subtraction operator for strings, so when js sees a subtraction operator:

“100” - 50

the only logical conclusion is that you are trying to subtract two numbers, so js converts the string to a number, and performs the subtraction. So, to convert a string to a number, you can subtract 0 from it.

(“100” - 0) + 50

Similarly, you can convert a string to a number by multiplying by 1:

(“100” * 1) + 50

However, it is not always clear what you are doing, so it’s better style to use parseInt(), parseFloat() or Number() to convert the string to a number. parseInt() and parseFloat() will try convert a string to number if it has trailing letters:

parseInt(“123abc”) //123

but will return NaN if there are leading letters:

pareseInt(“abc123”) //NaN

Number() returns NaN if there are any letters in the string. So, depending on what behaviour you want, you can use the appropriate function. In addition, it may prove useful to be able to test if the conversion to a number worked, and you have to use isNaN() for that:

var value=document.getElementById(“someId”).value;

var result = Number(value);

if(isNaN(result)) alert(“not a number–bad input”)