I have some experience in Java but I am new to JavaScript. What confuses me is that you don't have to declare the type of a variable when defining it. Here is an example of how this is sometimes hard for me to work with :
getInt(10) returns 105
getInt(15) returns 10
Is there a clean way to make sure that anInteger + 5 returns 15 and not 105 ? I use anInteger - 0 + 5 now, but I'd like to know if there is a better way to do it.
Code:
function getInt(anInteger) {
if (anInteger == 10) {
return anInteger + 5;
}
else {
return anInteger - 5;
}
}
getInt(10) returns 105
getInt(15) returns 10
Is there a clean way to make sure that anInteger + 5 returns 15 and not 105 ? I use anInteger - 0 + 5 now, but I'd like to know if there is a better way to do it.