Monday, April 4, 2005

Technical Javascript question

Originally posted on: http://geekswithblogs.net/meshel/archive/2005/04/04/28210.aspx

Due to the success I had in the past asking technical questions in this blog, I will make another attempt, please note that I looked around before asking, but was unable to come up with an answer by myself. This question was asked by one of my colleags here.

In Javascript you can assign a number to a var in the normal way
var x;
var y;
y = 3;
alert(y); //(shows 3)
y=4+6;
alert(y); //(shows 10)
//And so on, math seems to work pretty well.

//But something else that works is:
x = [3];
alert(x); //(shows 3)

x=[3+7];
alert(x); // (shows 10)

//But here is the interesting part:
alert(y+x); // (shows 1010)

I haven't found anything about this in documentation, it seems that though until that point it does treat them as numbers, the fact that x is enclosed in [] turns the result into a string. Can anyone give a complete explanation about this feature?


No comments: