// An array containing numbers, strings, and a function: var sampleArray = [ 5, 1, 'hello', function(x) { return x+5 }, -4, 'bye', 0 ]
// An array containing the number 6, the array [4,3,'hi'], and the nested array [3,[2,1]]: var nestedArray = [ 6, [ 4, 3, 'hi' ], [ 3, [ 2, 1 ] ] ]
var sampleArray = [ 5, 1, 'hello', function(x) { return x+5 }, -4, 'bye', 0 ] console.log( sampleArray[0], sampleArray[2], sampleArray[3] )would result in
var powerFunctions = [ function(x){return x^0}, function(x){return x}, function(x){return x*x}, function(x){return x*x*x}, ] var fourSquared = powerFunctions[2](4) // passes value 4 to third function in array console.log( fourSquared )would result in
var anotherArray = [ 2, 4, 6, 7, 10 ] anotherArray[3] = 8 anotherArray[4] = 'ten' console.log( anotherArray )would result in
var dogBreeds = ['boxer', 'rottweiler', 'afghan', 'silky terrier', 'papillon', 'chow chow', 'golden retriever' ] var numberOfDogs= dogBreeds.length console.log( numberOfDogs )would result in
var evens = [ 2,4,6,8,10,12,14,16,18,20 ] var sum = 0 for ( i=0; i<evens.length; i++ ) { sum = sum + evens[i] } console.log( "The sum of all even numbers from 1 to 20 is "+sum )would result in
var anotherArray = [ 2, 4, 6, 7, 6, 8, 11, 10 ] anotherArray.indexOf(6) // returns 2
var anotherArray = [ 2, 4, 6, 7, 6, 8, 11, 10 ] anotherArray.lastIndexOf(6) // returns 4
var dogBreeds = ['boxer', 'rottweiler', 'afghan', 'silky terrier', 'papillon', 'chow chow', 'golden retriever' ] dogBreeds.push( 'basset hound' ) console.log( dogBreeds ) dogBreeds.push( 'poodle', 'beagle' ) console.log( dogBreeds )would result in
var dogBreeds = ['boxer', 'rottweiler', 'afghan', 'silky terrier', 'papillon', 'chow chow', 'golden retriever' ] dogBreeds.unshift( 'dachshund' ) console.log( dogBreeds )would result in
var someNumbers = [ 4, 8, 10, 17, 20 ] var firstEntry = someNumbers.shift() console.log( someNumbers, firstEntry )would result in
var someNumbers = [ 4, 8, 10, 17, 20 ] var lastEntry = someNumbers.pop() console.log( someNumbers, lastEntry )would result in
var evens = [ 2, 4, 6, 8, 10 ] var odds = [ 1, 3, 5, 7, 9 ] var allNumbers = evens.concat(odds) console.log( allNumbers )would result in
function areTheyEven(element, index, array) { return element%2 == 0 } var test1 = [ 2, 4, 6, 8, 10 ].every(areTheyEven); var test2 = [ 3, 6, 2, 1, 12 ].every(areTheyEven); console.log( test1, test2 )would result in
function areTheyEven(element, index, array) { return element%2 == 0 } var test1 = [ 2, 3, 5, 7 ].some(areTheyEven); var test2 = [ 3, 5, 7 ].some(areTheyEven); console.log( test1, test2 )would result in
function areTheyEven(element, index, array) { return element%2 == 0 } var justEvens = [ 1,2,3,4,5,6,7,8,9,10 ].filter(areTheyEven); console.log( justEvens )would result in
function plusOne(element, index, array) { array[index] = element+1; } var smallArray = [1,2,3] smallArray.forEach(plusOne); console.log(smallArray)would result in
var smallNumbers = [ 1,2,3,4,5,6,7,8,9,10 ] var perfectSquares = smallNumbers.map( function(x){ return x*x } ) console.log(perfectSquares)would result in
var someNumbers = [ 0, 1, 2, 3, 4, 5 ] var sum = someNumbers.reduce( function(previousValue, currentValue, index, array) { console.log(currentValue) return previousValue + currentValue; }) console.log( sum )would result in
var grades = [ 84, 89, 92, 78, 95, 67, 81 ] var totalPoints = grades.reduce( function(previousValue, currentValue, index, array) { return previousValue + currentValue; }) var average = totalPoints/grades.length console.log(totalPoints, average)would result in
var someNumbers = [ 0, 1, 2, 3, 4, 5 ] var sum = someNumbers.reduceRight( function(previousValue, currentValue, index, array) { console.log(currentValue) return previousValue + currentValue; }) console.log( sum )would result in
var mixedArray = [ 1, 'hello', 3, 'dogs', 4+5, 0 ] var mixedString = mixedArray.join(separator='...') console.log(mixedString)would result in
var smallNumbers = [ 1, 2, 3, 4, 5, 6 ] smallNumbers.reverse() console.log( smallNumbers )would result in
var mixedNumbers = [ 5, 1, -7, 18, 30, -11, 0, Math.PI, 9, 3, -2 ] mixedNumbers.sort() console.log( mixedNumbers )would result in
var mixedArray = [ 1, 'hello', 3, 'dogs', 4+5, 0 ] mixedArray.sort() console.log( mixedArray )would result in
var mixedArray = [ 1, 'hello', 3, 'dogs', 4+5, 0 ] var mixedString = mixedArray.toString() console.log(mixedString)would result in
var arrayName = ['It is now', Date()] var stringName = arrayName.toLocaleString() alert( stringName )Try it
// Add a new dog to the array after the second element var dogBreeds = ['boxer', 'rottweiler', 'afghan', 'silky terrier', 'papillon' ] dogBreeds.splice( 2, 0, 'corgi') console.log( dogBreeds )would result in
// Remove two elements from the array, starting after the second element var dogBreeds = ['boxer', 'rottweiler', 'afghan', 'silky terrier', 'papillon' ] dogBreeds.splice( 2, 2 ) console.log( dogBreeds )would result in