Moshe Eshel

The eye in the sky - just fell down

  Home  |   Contact  |   Syndication    |   Login
  69 Posts | 0 Stories | 5 Comments | 57 Trackbacks

News

Welcome to my blog, I hope to have here some interesting content for you all to read. However, I'm just starting out in this, so be patient with me.

Article Categories

Archives

Post Categories

Blogroll

News Sites

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?


 

posted on Monday, April 04, 2005 6:03 PM

Feedback

# re: Technical Javascript question 4/4/2005 6:30 PM George
Since you are not specifying the index of the array, it will do a conversion toString() of its content.

Try alert(y+x[0]) and you'll get the expected result
or
Try expanding the array to x=[1,2,3] and you'll get "101,2,3"

HTH

# re: Technical Javascript question 4/4/2005 6:44 PM BradC
Ah, so its an implicit 1-dimentional array.
That makes sense.

# re: Technical Javascript question 4/4/2005 6:48 PM Moshe Eshel
I figured it was something like that, but it seems unnatural for the interperter to work in this way.
+ it is not documented in any place which is even worse. Normal programming languages would have thrown an error without thinking twice.

Still if someone can direct me to some docs on this I would be gratefull.

# re: Technical Javascript question 4/4/2005 8:11 PM George
oneValue = "test";
oneArray = [1,2,3];
oneObject = {key1:'value1', key2:'value2'};

write(oneValue);
write(oneArray[0]);
write(oneObject['key1']);
write(oneObject.key1);

mix at will and drink responsibly ;-)

Ps. you can download the full specs from the ecmascript.org site for the 262 and the newest 357 E4X versions.

http://www.ecma-international.org/publications/standards/Stnindex.htm


# re: Technical Javascript question 4/4/2005 8:15 PM George
Remember JS is very forgiving, it's not meant to interrupt the flow of your surfing.

Fail gracefully is their motto ;-)


# re: Technical Javascript question 6/12/2005 12:01 PM Towhid Ahammed Chowdhury
Well, When U assign a Value in a variabl using [], it behave like a integer Value.

But When U calculate a numeric value with another Object which was created by [], it treat like a string. try with out [] that will be a numeric result.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: