| | |
Question about arrays
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 25
Reputation:
Solved Threads: 0
Hello everyone, I just have a quick Javascript question I haven't found an answer to on the net. What is the difference between
Thank you,
Jmasta
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.
Current best-practice eschews the "new" keyword on Javascript primitives. If you want to create a new Array simply use brackets [] like this...
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.
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.
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)
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.
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
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/
arrayName.item(0)
it is referring to something in the DOM, for example:
http://jacksleight.com/blog/2008/01/14/getelementsby/
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
Join Date: Apr 2007
Posts: 25
Reputation:
Solved Threads: 0
I'm sorry, I should have been more clear. Let's say I already did something like
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.
var pageInputs = getElemetsByTagName("input"); Now, i have a collection, pageInputs. So what's the difference betweenconsole.log(pageInputs[0]) andconsole.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.
•
•
Join Date: Aug 2008
Posts: 381
Reputation:
Solved Threads: 33
•
•
•
•
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/
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<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
Last edited by langsor; Aug 19th, 2008 at 1:46 am.
•
•
Join Date: Aug 2008
Posts: 381
Reputation:
Solved Threads: 33
•
•
•
•
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.
...
![]() |
Similar Threads
- C++ Char question (C++)
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- Newbie Problem: Arrays (C++)
- Question on arrays & methods (Java)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- array question (C++)
- Simple (I think) Code Question (PHP)
- Help with Arrays needed! (C++)
- Completely new to C++ and have question about using char (C++)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: 2 seperate textbox validation with 2 buttons
- Next Thread: img rollover help
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxcode ajaxhelp animate api automatically beta boarder box bug calendar card checkbox child class column cookies createrange() css cursor dependent disablefirebug dom download dropdown editor element engine error events explorer ext file firehose flash form forms game google gwt html htmlform ie8 iframe image() images internet java javascript jawascriptruntimeerror jquery jsf jsfile jump math matrixcaptcha microsoft mimic mp3 mysql object offline onmouseoutdivproblem onreadystatechange parent passing pdf php player post problem progressbar rated rating regex runtime scroll search select session shopping size sql star stars stretch text textarea twitter validation w3c web website window windowofwords windowsxp wysiwyg xml xspf \n





