May 2, 2002 - The Risk of Un-typed Variables | WebReference

May 2, 2002 - The Risk of Un-typed Variables

Yehuda Shiran May 2, 2002
The Risk of Un-typed Variables
Tips: May 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

JScript .NET's support for un-typed variables makes it very easy to develop short scripts. Novice and occasional developers can focus more on the business logic, rather than being concerned with what types their variables are. The drawback to using un-typed variables is that the results are not always what you would expect. Let's take the following example:

  var x, y, z;
  x = "2";
  y = 2;
  z = x + y;
What do you think the value of z will be? Since we did not declare the types of x and y, the compiler casts one of them to the other. In the case above, z will be 22. The variable y is casted to String and concatenated with x.

To learn more on JScript .NET, go to Column 108, JScript .NET, Part II: Major Features.