Skip to content

Instantly share code, notes, and snippets.

@vishalkukreja
Created March 29, 2021 17:50
Show Gist options
  • Save vishalkukreja/3ad7139764b008700fdfddd3cc4ebf3c to your computer and use it in GitHub Desktop.
Save vishalkukreja/3ad7139764b008700fdfddd3cc4ebf3c to your computer and use it in GitHub Desktop.
// Basic array
var cityNames = ['Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore'];
console.log("City names:", cityNames);
// Output : City names: [ 'Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore' ]
console.log("City at 2nd index:", cityNames[2]);
//Add an item to an array
cityNames.push('Jaipur');
console.log("City names:", cityNames);
//Output: City names: [ 'Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore', 'Jaipur' ]
//Remove an item from an array
cityNames.pop();
console.log("City names:", cityNames);
//Output: City names: [ 'Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore' ]
//Remove an item from the beginning of an Array
cityNames.shift();
console.log("City names:", cityNames);
//Output: City names: [ 'Delhi', 'Pune', 'Ahmadabad', 'Indore' ]
//Add an item to the beginning of an Array
cityNames.unshift('Chennai', 'Mumbai');
console.log("City names:", cityNames);
//Output: City names: [ 'Chennai', 'Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment