• Tutorials Logic, IN
  • +91 8092939553
  • info@tutorialslogic.com

Arrays in JavaScript

Arrays

An array is a special variable, which allow us to store multiple values together in a single variable of same or different data types. It can store any valid value, which include string, number, object, function, and even other array.

Creating an Array

										    
											const fruits = ["Apple", "Mango", "Orange"];
											
										

It can also be created using new keyword like below-

										    
											const fruits = new Array("Apple", "Mango", "Orange");
											
										

Important Array Methods

The slice() Method

The slice() method returns a shallow copy of a part of an array, as a new array object. It can accept two arguments start and end (end not included), where start and end represent the index of an array. The original array will not be changed.

										    
											const fruits = ["Apple", "Mango", "Orange"];
											fruits.slice(1); // Expected output: ["Mango", "Orange"]
											fruits.slice(1, 2); // Expected output: ["Mango"]
											fruits.slice(-2, -1); // Expected output: ["Mango"]