Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript array declaration: new Array(), new Array(3), ['a', 'b', 'c'] create arrays that behave differently

Consider this example Javascript code:

a = new Array(); a['a1']='foo'; a['a2']='bar';  b = new Array(2); b['b1']='foo'; b['b2']='bar';  c=['c1','c2','c3'];  console.log(a); console.log(b); console.log(c); 

Results in the Firebug console are as follows:

For a (the '[]' had to be expanded by clicking on the '+' button):

[]       a1  "foo"    a2  "bar" 

For b:

[undefined, undefined] 

For c:

["c1", "c2", "c3"] 

My questions are:

  1. Am I using the array['key']='value' syntax correctly?
  2. Why isn't array b working as expected?
  3. Why are arrays a and c displayed differently in the console? It also seems that jQuery is unable to iterate through the array a with it's .each() method.
  4. Could you reccomend any good tutorials on Javascript array behaviour?

NOTE: Google Chrome's Firebug displays only [] for array 'a', without the option to expand it.

EDIT: Alright, it seems that arrays in Javascript have only numerical keys, so adding a string as a key name makes an object out of an array. But why doesn't jQuery's .each work with it?

$.each(a, function ()     {     alert ('derp');     }) 

This code, appended to the script, produces no alerts.

like image 803
sbichenko Avatar asked Nov 23 '11 17:11

sbichenko


People also ask

What is the correct way to create a JavaScript array with 3 items?

The Array constructor has following three forms. Syntax: var arrayName = new Array(); var arrayName = new Array(Number length); var arrayName = new Array(element1, element2, element3,... elementN);

How do you declare array in JavaScript?

Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

What is array () in JavaScript?

In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable.

What is {} and [] in JavaScript?

{} is shorthand for creating an empty object. You can consider this as the base for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array . [] is shorthand for creating an empty array.


1 Answers

Arrays have numerical indexes. So,

a = new Array(); a['a1']='foo'; a['a2']='bar';  and  b = new Array(2); b['b1']='foo'; b['b2']='bar'; 

are not adding elements to the array, but adding .a1 and .a2 properties to the a object (arrays are objects too). As further evidence, if you did this:

a = new Array(); a['a1']='foo'; a['a2']='bar'; console.log(a.length);   // outputs zero because there are no items in the array 

Your third option:

c=['c1','c2','c3']; 

is assigning the variable c an array with three elements. Those three elements can be accessed as: c[0], c[1] and c[2]. In other words, c[0] === 'c1' and c.length === 3.

Javascript does not use its array functionality for what other languages call associative arrays where you can use any type of key in the array. You can implement most of the functionality of an associative array by just using an object in javascript where each item is just a property like this.

a = {}; a['a1']='foo'; a['a2']='bar'; 

It is generally a mistake to use an array for this purpose as it just confuses people reading your code and leads to false assumptions about how the code works.

like image 136
jfriend00 Avatar answered Sep 24 '22 05:09

jfriend00