9
ARRAYS JavaScript 101 with Professor Flo

Working with Arrays in JavaScript

Embed Size (px)

Citation preview

ARRAYSJavaScript 101

with Professor Flo

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

ARRAY METHODS

VROOM VROOM next up different Methods or

Functions that can be performed on arrays

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

ARRAY MUTATIONSFor the Gold !!!

ARRAY HELPERS

• var name = [‘Bob Marley’, ‘Led Zeplin’, ‘Zig Ziglar’];

• name.sort();// sort the array of names,

• name.reverse(); // reverse the order of the array

• You can find additional helpers on problem 7 Year Up Chrome Extension