Monday, October 29, 2007

JavaScript type casting

here is one... So I'm doing some silverlight and I try to add two 'numbers' right. Javascript is supposed to be not strongly typed language but there are cases where you need to type caste... which normally only shows up in real languages or um strongly typed langauges like say C#. In JavaScript 'type casting' is a bit mucked up but basically there are two types you can try to force things to 'string' or 'number'. The code looks something like this:

var SomeValue = "100";
var SomeNumber = 3;

// alert 1: "1003"
alert( SomeValue + SomeNumber);

// alert 2: "103"
alert( Number(SomeValue) + SomeNumber);

So this is where we can get a way with telling people 1 * 1 may not always equal 2... in this sample the first alert is going to make everything a string where as in the next one we cast the questionable value to a Number and now the math works right...

hmmm... whats the point of a varient if it can't figure out its a number.

Anyway so there is type casting in JavaScript

3 comments:

  1. Ok, I'm feeling like I think something this morning so here goes. In the last comment I read: "So this is where we can get a way with telling people 1 * 1 may not always equal 2." It never does =2 does it? Just for laughs.

    I personally don't see anything "mucked up" about javascript type casting, except that you seldom need it. I think it's a simple matter of object creation, like some functional languages I've studied. Call the constructor to the 'String' or 'Number' class and associate the relevant methods and/or attributes with the object created.

    And for me the difference between strongly typed languages and "loosely typed" languages is a little mucked up. The difference seems to me to be whether or not the typing is done on the hood or under the hood. In Scheme, for example, you don't have to declare a variable or its type, yet it is considered strongly typed, because, at run time, an exception will be thrown if the interpreter finds an illegal operation on dissimilar types. Maybe I'm all wet, here, but this is my understanding and I think it applies to javascript, as well. Everything, including the number '1', is an object with well defined attributes and methods. It's just that the programmer is spared the responsibility of 'always' having to explicitly decide which contructor is called an how the language element will behave under the given circumstances. The interpreter is written to be as intuitive as possible in this respect and to produce predictable results without explicit declarations, but the typings and conversions are still always going on "under the hood".

    Just my .25 cents. Feel free to criticize.

    ReplyDelete
  2. I think the point is you shoudl either have todo it or not. it should be strongly typed or not. like a good language like C++ where things are one way or not at all. In my opinion structured strong typed languages are easier to deal with as the rules are more cut and dry.

    ReplyDelete
  3. All ECMAScript dialects (JavaScript, JScript ActionScript, etc.) support typecasting using the type objects as above or using literal constructs. Examples:

    myBoolean1 = Boolean(123);
    myBoolean2 = !! 123;
    // Both == true

    myString1 = String(123);
    myString2 = "" + 123;
    // Both == "123"

    myNumber1 = Number("123");
    myNumber2 = 1 * "123";
    // Both == 123

    ReplyDelete