JavaScript Array Methods()...7

JavaScript Array Methods()...7

·

3 min read

Hellooo and Welcome to article #7 in the series JavaScript Array Methods(),

In this article, we're going to learn the basics of some array methods in JavaScript(syntax, purpose, as well as example snippets),

In this article we are going to learn about the following array methods:

  • .slice(),
  • .splice(),
  • .toString(),
  • .valueOf(),

If you are new here and want to know some more array methods, I have a series on JavaScript Array Methods(), you can check it out here

Let's get started with array methods-

.splice()

  • Using the .splice() method you can do the following things:-

    • Remove one or more items from an array,
    • or you can add/insert one or more items to an array,
    • or you can replace one or more items from an array and add more items at the same time as well,
  • It returns an array of removed items,

  • And it makes changes to the original array,

Syntax

arrayName.splice(startIndex, deleteCount, item-1, ...., item-n)
  1. arrayName - the array to apply the method on,

  2. startIndex - the index position to start adding/deleting elements,

  3. deleteCount- (optional)number of elements to delete if omitted all the elements after the startIndex to the end of the array will be deleted,

  4. item-1, ...., item-n - (optional)items to add/insert into the array,

Example Snippet

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let removed = numbers.splice(3, 1, 89, 90);
console.log("arr" + numbers);
console.log("removed" + removed);
// arr 1,2,3,89,90,5,6,7,8,9
// removed 4

.slice()

  • You can use this method whenever you need to extract(copy to be specific) one or more elements from the array when you already know the indexes of those elements,

  • It doesn't make changes to the original array,

  • It returns an array of items copied from the array

    Syntax

    arrayName.slice(startIndex, endIndex)
    
  • arrayName - the array you want to copy items from,

  • startIndex - index position to start getting items from

  • endIndex - end position to stop copying items excluding the item on end index,

  • Both the start and the end indexes are optional if you omit them 0 will be considered as the start index and array.length - 1 as end index,

Example Snippet

const userNames = ["Casey", "Danny", "Joe", "Adam", "Eva"];
let copiedArray = userNames.slice(1, 3);
console.log(copiedArray);
// ["Danny", "Joe"]

.toString()

  • This method returns a string containing all the array elements from an array separated by commas (,),

  • It does not make changes to the initial array,

Syntax

arrayName.toString();
  • It doesn't require any parameter,

Example Snippet

const userNames = ["Casey", "Danny", "Joe", "Adam", "Eva"];
let arrayString= userNames.toString();
console.log(arrayString);
//  Casey,Danny,Joe,Adam,Eva

.valueOf()

  • Let's take a look at why we need this method first, We all can use(printing or assigning to other arrays or some other similar things) all the elements of an array by just directly putting the array name wherever we need to use that array,

  • But JavaScript has a method for it too,

  • This is the .valueOf() method, it returns the elements of the array with no changes,

  • It also doesn't make any changes to the original array,

Syntax

array.valueOf()
  • It does not require any parameters,

Example Snippet

const userNames = ["Casey", "Danny", "Joe", "Adam", "Eva"];
let anotherArray= userNames.valueOf();
console.log(anotherArray);
// ["Casey", "Danny", "Joe", "Adam", "Eva"]

Yay, you made it to the end of the array methods for now,

And thanks for reading so far, Hope you guys like it, If you really like it, Please lemme know in the comments and do hit the like buttons on the top-right corner And stay tuned, I am going to post some nice stuff in the future,

Till then bye-bye, take care, Stay home, stay safe, stay negative and remember each and every life matters