943,685 Members | Top Members by Rank

Ad:
Aug 18th, 2008
0

Question about arrays

Expand Post »
Hello everyone, I just have a quick Javascript question I haven't found an answer to on the net. What is the difference between
arrayName[0] and
arrayName.item(0) I've run across instances of both, but nowhere have I seen the difference between the two.
Thank you,
Jmasta
Last edited by jmasta; Aug 18th, 2008 at 1:50 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jmasta is offline Offline
26 posts
since Apr 2007
Aug 18th, 2008
0

Re: Question about arrays

Current best-practice eschews the "new" keyword on Javascript primitives. If you want to create a new Array simply use brackets [] like this...



var myArray = [];


You don't need to tell Javascript how many items to size the Array for. Javascript will automatically increase the size of the Array as needed, as you add items into the Array. Creating an Array with brackets instead of with the "new" constructor avoids a bit of confusion where you want to initialize only one integer. For instance.




JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var badArray = new Array(10); // Creates an empty Array that's sized for 10 elements.
  2.  
  3.  
  4. var goodArray= [10]; // Creates an Array with 10 as the first element.



As you can see these two lines do two very different things. If you had wanted to add more than one item then badArray would be initialized correctly since Javascript would then be smart enough to know that you were initializing the array instead of stating how many elements you wanted to add.
Since the new constructor is not necessary with Arrays and there's a slight chance of unintended results by using the new constructor, it's recommended you not use "new Array()" to create an Array.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Aug 18th, 2008
0

Re: Question about arrays

Usually when you see something like this:
arrayName.item(0)

it is referring to something in the DOM, for example:
http://jacksleight.com/blog/2008/01/14/getelementsby/
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Aug 19th, 2008
0

Re: Question about arrays

I'm sorry, I should have been more clear. Let's say I already did something like
var pageInputs = getElemetsByTagName("input"); Now, i have a collection, pageInputs. So what's the difference between
console.log(pageInputs[0]) and
console.log(pageInputs.item(0))
I haven't had a chance to test them, but wouldn't they return the same thing? I just hadn't seen the '.item()' syntax before.
Reputation Points: 10
Solved Threads: 0
Light Poster
jmasta is offline Offline
26 posts
since Apr 2007
Aug 19th, 2008
0

Re: Question about arrays

Click to Expand / Collapse  Quote originally posted by R0bb0b ...
Usually when you see something like this:
arrayName.item(0)

it is referring to something in the DOM, for example:
http://jacksleight.com/blog/2008/01/14/getelementsby/
R0bb0b is correct ... item(0) accesses a node-collection
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2.  
  3. arr = [];
  4. arr[0] = 'one';
  5. arr['two'] = 3;
  6.  
  7. alert( arr[0] );
  8. alert( arr['two'] );
  9.  
  10. $links = document.getElementsByTagName('a');
  11. for ( var i = 0; i < links.length; i ++ ) {
  12. alert( links.item(i).href );
  13. }
  14. </script>

as an example of different arrays / node collections
Last edited by langsor; Aug 19th, 2008 at 1:46 am.
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008
Aug 19th, 2008
0

Re: Question about arrays

Click to Expand / Collapse  Quote originally posted by jmasta ...
I'm sorry, I should have been more clear. Let's say I already did something like
var pageInputs = getElemetsByTagName("input"); Now, i have a collection, pageInputs. So what's the difference between
console.log(pageInputs[0]) and
console.log(pageInputs.item(0))
I haven't had a chance to test them, but wouldn't they return the same thing? I just hadn't seen the '.item()' syntax before.
Yes, they would return the same thing and are effectively the same, but the .item(0) is the correct syntax for DOM node-collections, but the array access syntax is cross-browser supported.

...
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008
Aug 19th, 2008
0

Re: Question about arrays

Thank you folks, and thanks Langsor. That's what I was thinking, but just wanted to make sure.
Reputation Points: 10
Solved Threads: 0
Light Poster
jmasta is offline Offline
26 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: 2 seperate textbox validation with 2 buttons
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: img rollover help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC