Post on 06-Aug-2015
CREATING ARRAYS
• var a = [ ]; // this creates an empty
• var numbers = [1,2,3,4,5,6]; // this creates an array of numbers
ACCESSING ARRAY ITEMS
• How to Access a Single Element in an array
• Example var friends = [‘Florence’, ‘Seth’, ‘Richard’];
• friends[0]; // evaluates to Florence
• friends[2]; // evaluates to Richard
THE LENGTH PROPERTY
• The Length; property gives the array length
• var friends = [‘Florence’, ‘Seth’, ‘Hunter’];
• friends.length; // evaluates to 3
PUSH AND UNSHIFT
• var cars = [‘BMW’, ‘Lexus’, ‘Toyota’, ‘Honda’];
• cars.push(‘Tesla’, ‘Jaguar’); // Push properties adds elements to the end of the array.
• cars.unshift(‘Jeep’, ‘Ford’); // Adds items to the front of the array.
POP AND SHIFT
• Both Pop and Shift Methods Remove Items from the array
• var fruits = [‘Apples’, ‘Oranges’, ‘Mangos’, ‘Grapes’];
• fruits.pop(); // removes the last item from the array, i.e. grapes
• fruits.shift(); // removes the first item from the array, ie. removes Apples