Arrays in JavaScript

From [0] to [ ‘H’, ‘E’, ‘R’, ‘O’ ]

Nikhil Swain
May 30 · 8 min read
var fruit0 = 'Mango';
var fruit1 = 'Apple';
var fruit2 = 'Orange';
Var fruits = new Array('Mango','Apple','Orange');

Declaration of an Array

var array = [1, 2, 3]
var array = new Array(1, 2, 3)
var number = [1, 2, 3, 4, 5]
var strings = ['name', 'place', 'food' ]
var functions = [ function x(){...}, finction y(){...} ]
var mix = [ 12, 'niceplace', function xy(){...} ]
var array = [ 'val1', 'val2', 'val3']
alert(array[0]); // alerts val1.

Creating an Array of Arrays

var Avengers = [
    [ 'ironman', 'dr.strange', 'cap.america' ],
    [ 'blackwidow', 'scarletwitch', 'wasp' ]
];
var Avengers = new Array(
   new Array('ironman', 'dr.strange', 'cap.america'),
   new Array('blackwidow', 'scarletwitch', 'wasp')
);

Indexing an Array with Strings Instead of Numbers

var Avengers = [];
Avengers['males'] = [ 'ironman', 'dr.strange', 'cap.america' ];
Avengers['females'] = [ 'blackwidow', 'scarletwitch', 'wasp' ];

Turning an Array into a String.

By join() method

var Avengers = [ 'ironman', 'dr.strange', 'cap.america' ];var together = Avengers.join('');
alert(together); var list = Avengers.join();
alert(list);var sentence = Avengers.join(' and ')
alert(sentence);
var Avengers = [ 'ironman', 'dr.strange', 'cap.america' ];var together = Avengers.toString();alert(together); // ironman,dr.strange,cap.america

Adding or Removing Members from an Array

Changing/Replacing an existing value with a new one.

var Coffee = ['Americano', 'Cappuccino', 'Expresso', 'Latte'];
Coffee[0] = 'Mocha';
alert(Coffee);
var Coffee = ['Americano', 'Cappuccino', 'Expresso', 'Latte'];
Coffee[4] = "Macchiato";
alert(Coffee);
var Coffee = ['Americano', 'Cappuccino', 'Expresso', 'Latte'];
Coffee[5] = "Macchiato";
alert(Coffee);
empty value at index 4.

Adding Members : The push() and unshift() method

var DBZ = ['Goku', 'Vegita', 'Gohan', 'Goten'];
DBZ.unshift("Frieza");
alert(DBZ);
var DBZ = ['Goku', 'Vegita', 'Gohan', 'Goten'];
DBZ.push("Trunks");
alert(dbz);

Removing Members : The pop() and shift() method

var Naruto = ['Naruto', 'Sasuke', 'Sakura', ' Hinata'];
Naruto.shift();
alert(Naruto);
var Naruto = ['Naruto', 'Sasuke', 'Sakura', ' Hinata'];
Naruto.pop();
alert(Naruto);

Understanding splice() and slice() methods

Syntax:

Array.splice( index, remove_count, item_list )
var Pokemon = [ 'Pikachu', 'Bulbasaur', 'Charmander', 'Squirtle', 'Caterpie' ];Pokemon.splice(2); // remove ["Charmander", "Squirtle", "Caterpie"]alert(Pokemon); // Pikachu,Bulbasaur
var Pokemon = [ 'Pikachu', 'Bulbasaur', 'Charmander', 'Squirtle', 'Caterpie' ];Pokemon.splice(2,1);alert(Pokemon); // Pikachu,Bulbasaur,Squirtle,Caterpie
var Pokemon = [ 'Pikachu', 'Bulbasaur', 'Charmander', 'Squirtle', 'Caterpie' ];Pokemon.splice(2,0,'Evolve');alert(Pokemon);  
// Pikachu,Bulbasaur,Evolve,Charmander,Squirtle,Caterpie

Syntax :

Array.slice(start, end)
var life = ['eat', 'sleep', 'code', 'repeat'];var slice = life.slice(2);alert(slice); // code,repeatalert(life) // eat,sleep,code,repeat 
var life = ['eat', 'sleep', 'code', 'repeat'];var slice = life.slice(1,3);alert(slice) // sleep,code
checking presence of item in an array by includes() method

Sorting an Array into Alphabetical or Numerical Order

var songs = [’Voices’, ’Wasteland’, ’Gravity’, 'Scream’, 'Runaway’];songs.sort();alert(songs);
var score = [25, 8, 5, 2, 4, 1, 7, 1995];
score.sort(function(a,b){
return a-b } 
);

Reversing an Array

Syntax :

Array.reverse();
var Avengers = [
    [ 'ironman', 'dr.strange', 'cap.america' ],
    [ 'blackwidow', 'scarletwitch', 'wasp' ]
];Avengers.reverse();alert(Avengers); 
// blackwidow,scarletwitch,wasp,ironman,dr.strange,cap.america
original Array
Array after reversing.

The Startup

Medium's largest active publication, followed by +469K people. Follow to join our community.

Nikhil Swain

Written by

!YouKnowMe(){ if(!today){ "ThenYouWillKnowmaybeSomeOtherDay" } }();

The Startup

Medium's largest active publication, followed by +469K people. Follow to join our community.