Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // An array is an ordered list of items, with a starting index of 0.
- var array = [];
- // foo.push(bar) adds item bar to the end of array foo.
- array.push('cat');
- array.push('dog');
- array.push('bird');
- console.log(array.length); // 3
- console.log(array[0]); // cat
- console.log(array[2]); // bird
- array.push('fish'); // Push 'fish' to 'array'
- console.log(array.length); // 4
- // Empty the array-delete everything in it.
- array = [];
- console.log(array.length); // 0
- array = ['cat', 'dog', 'bird', 'fish']; // Same as push, except all at once.
- console.log(array.length); // 4
- // A multidimensional array is an array with an array.
- // Rather than a single item at each index, there is an array at each index.
- // Same as above, except each array item is an array.
- array = [
- ['cat', 'dog', 'bird', 'fish'],
- ['apple', 'orange', 'melon', 'pear'],
- ['blue', 'green', 'red', 'yellow']
- ];
- console.log(array[0][1]); // dog
- console.log(array.length); // 3
- console.log(array[1].length); // 4
- console.log(array[2][2]); // red
- console.log(array.[2][2].length); // 3 (3 characters in 'red')
- // Every language, programming or spoken, is different.
- // I mistakenly believed that a multidimensional array had to be declared as such.
- // So I declared:
- array = [[]];
- // This declared an empty array, and added an empty array as the first element.
Advertisement
Add Comment
Please, Sign In to add comment