var value = $("#myTextbox").val(); var calculation = value + (value * 0.05); alert(calculation);
When I typed
#myTextbox, I got an alert that said
10.05. I lost count of how many times I double checked my math.
val() method returns a string. My
calculation variable was being assigned like this:
(value * 0.05),where
valueis implicitly cast as a number.
valueto the result of the above evaluation, where that result is cast as a string.
From what I’ve been able to gather after a little digging, the
+ operator will cast one operand as a string if the other operand is a string, regardless of their order. Similarly, the
* operator will cast both operands as numbers all the time (I even tried this with boolean values).
The Benefits of Strong Typing
Draw Your Own Conclusions
The important thing to keep in mind is that you should always choose the tools that are appropriate for your task. If you’re building an application for the enterprise or a safety critical system, you will definitely want a language with strong typing so that you can take advantage of the additional static checking it gives you. If you’re building a web site for your kid’s baseball team, maybe that’s not so important. At the end of the day, we all have our preferences, but a strong software engineer must make smart choices based on the needs of the project.