Arrays with JavaScript
Video
JavaScript Notes
JavaScript
var characters = ['Luke', 'Leia', 'Han', 'Chewie'];
characters.splice(0, 1, 'Rey'); //removes 1 item from position 0, and adds 'Rey'
characters.splice(1, 2, 'Kylo'); //removes 2 items from position 1, and adds 'Kylo'
characters.push('BB8'); //adds 'BB8' to the top
characters.push('Obiwan'); //adds 'Obiwan' to the top
characters.reverse(); //reverses the order of the array
characters.sort(); //sorts the array in alphabetical order
var position = characters.indexOf('Luke'); //looks for the first index position of 'Luke',
console.log(position); //returns the index position, or -1 if not found
var position = characters.indexOf('Leia'); //looks for the first index position of 'Leia'
console.log(position); //returns the index position, or -1 if not found
var position = characters.lastIndexOf('Han'); //looks for the last index position of 'Han'
console.log(position); //returns the index position, or -1 if not found
Feedback
Submit and view feedback