JavaScript Array Methods()...4

JavaScript Array Methods()...4

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

  • .isArray(),
  • .join(),
  • .keys(),
  • .lastIndexOf()

Hellooo and Welcome to article #4 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),

If you haven't read my previous articles on JavaScript Array Methods(), you can check them out here

now let's get started with array methods

.isArray()

  • forEach method is used to check whether a variable is an array or not,

  • Sometimes you will get some situations when you will need to perform some operation only on arrays or you need to check and make sure that the action will only be performed only in arrays or some similar kind of situations,

  • It returns true if the passed variable is an array otherwise false,

Syntax

Array.isArray(arrayName);
  1. arrayName - the array to be checked,

Example Snippet

var numbers = [1, 2, 3, 4];
Array.isArray(numbers);
// true

.join()

  • join method is used to copy array elements as a string,

  • It takes all the elements from the array and combines them into a string,

  • It takes the separator as a parameter and uses it to separate each element inside the string, the default separator is a comma (,)

  • It returns a string of the array elements,

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

Syntax

arrayName.join("separator");
  1. separator: (optional) It is used to separate the elements into a string (default is (,) ),

Example Snippet

var cars = ["Volvo", "BMW", "Jaguar", "Suzuki", "Tata"];
cars.join("-");
// "Volvo-BMW-Jaguar-Suzuki-Tata"

.keys()

  • keys() method returns an array Iterator Object or just say array to keep it simple, containing the keys(in arrays' case indexes) of all array/object elements,

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

Syntax

arrayName.keys();
  • No parameters required

Example Snippet

const userNames = ["Eddy", "Joe", "William", "Ratan", "Chris"];
const userKeys = userNames.keys();
for(let key of userKeys){
console.log(key);
}
// 0
// 1
// 2
// 3
// 4

.lastIndexOf()

  • lastIndexOf method is similar to the indexOf() method, both of the methods search for a specified element's index inside an array and returns the index if the element is found,

  • The key difference between them is that the lastIndexOf method begins its search from the end of the array and ends the search at the beginning of the array,

Syntax

arrayName.lastIndexOf(element, startIndex);
  1. element: the element, to find the index of,

  2. startIndex: (optional) index to where to start searching (default is array.length - 1),

Example Snippet

const userNames = ["Eddy", "Joe", "William", "Ratan", "Chris"];
userNames.lastIndexOf("Ratan");
// 3

That's it for now,

Thanks for reading so far,

see you in the next one(coming soon),

till then stay safe stay home stay negative take care Have a nice time ahead

and you can check out my other article on JavaScript Array Methods here