Array and String methods in JavaScript
Array methods:
Splice(): The splice() method is used to add or remove items to/from an array, and it returns the removed/added items as an array. Here you can mention the index of the array. The first argument is from where you want to start removing the item and the last argument here is where you want to end.
And if you want to remove only a single element from the array you just mention the index of that particular item in the array. For example,
var numArr = [‘Abul’, ‘Cabul’, ‘Babul’];
numArr.splice(1);
It will return [‘Cabul’].
The splice method actually changes the original array.
2. Join(): The Join() method converts an array to a string, here you can mention the separator by which the items of the string will be separated from each other.
3. toString(): This is pretty much similar to Join() method. But the default separator here is comma and it cannot be changed.
4. pop(): This method removes the last item from an array and returns the removed item as an string. This method mutate the original array. And you can hold the removed item in a variable if you want.
5. push(): The complete opposite method of pop() is the push() method. It adds a new item to an array at the last position of that array. You have to pass in the item which you want to add in that array as an argument of the push() method. This method also changes the original array and return the index position at where the new item is added which is obviously the last index of that array.
String methods:
- indexOf(): This method returns the index or the position of the first appearance of a particular text in a string.
2. Slice(): This method pulls out a particular string and outputs the extracted part in a brand new string. It takes two parameters: start and end index (excluded).
3. replace(): The method replaces a particular value with a new value in a string. It takes two argument the first one is the string that needs to be replaced and last one is the string that will replace the first one.
4. concat(): This method joins two or more strings into a single string.
5. trim(): Removes whitespace from both sides of a string.