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

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.

var badArray = new Array(10); // Creates an empty Array that's sized for 10 elements.


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.

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.

Member Avatar for langsor

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

<script type="text/javascript">

arr = [];
arr[0] = 'one';
arr['two'] = 3;

alert( arr[0] );
alert( arr['two'] );

$links = document.getElementsByTagName('a');
for ( var i = 0; i < links.length; i ++ ) {
  alert( links.item(i).href );
}
</script>

as an example of different arrays / node collections

Member Avatar for langsor

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.

...

Thank you folks, and thanks Langsor. That's what I was thinking, but just wanted to make sure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.