Skip to content

Instantly share code, notes, and snippets.

@AEnMe
Last active July 1, 2020 15:32
Show Gist options
  • Save AEnMe/c55f7e4490cbdcfd1d034acb3aa823fa to your computer and use it in GitHub Desktop.
Save AEnMe/c55f7e4490cbdcfd1d034acb3aa823fa to your computer and use it in GitHub Desktop.
Array Method
var array = [1,3,4,2];
// Output an array as a string separated with the argument's contents.
array.join(", ")
//1, 3, 4, 2
// Outputs a string with elements separated by commas.
array.toString()
//1,3,4,2
// Sort an array alphabetically or numerically.
array.sort()
//[1,2,3,4]
// Removes a set of indices from an array. First argument is the start point, second argument is the number indices to remove, third argument is optional, what to insert as a replacement.
array.splice(1, 2, "string")
//[3,4]
//[1,"string",2]
// Returns selected indices as a new array. First argument is the start point, second argument is the ending index.
array.slice(1, 3)
//[3,4]
// Reverses elements and returns the new array.
array.reverse()
//[2,4,3,1]
// removes first index in array and outputs that element. Reference the array variable again to see new changes.
array.shift()
//1
//[3,4,2]
// adds new item to start of array and returns new length. Reference the array variable again to see new changes.
array.unshift("string")
//5
//["string",1,3,4,2]
// removes last index in array and outputs that element. Reference the array variable again to see new changes.
array.pop()
//2
//[1,3,4]
// Adds a new item to end of array and returns new length. Reference the array variable again to see new changes.
array.push("string")
//5
//[1,3,4,2,"string"]
// Joins two arrays together into one array.
array.concat(array2)
//[1,3,4,2,"a","b"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment