Introduction- An Array is a special variable, which can hold more than one value. A JavaScript array is a data structure that allows you to store and organize multiple values in a single variable. It acts like a container that holds different elements, such as numbers, strings, objects or even other arrays. Each element in the array is assigned a unique index starting from 0, which allows you to access and manipulate individual elements. JavaScript arrays provide built-in methods that make it easy to perform operations like adding or removing elements, searching for specific values and iterating over the elements for processing. They are versatile tools for managing collections of data in JavaScript programming.
JavaScript Array Methods
Array length
Array toString()
Array pop()
Array Push()
Array shift()
Array unshift()
Array Join()
Array delete()
Array concat()
Array flat()
Array splice()
Array slice()
Array length - The length method of an Array represents the number of elements in that array.
const fruits = ['Krishna', 'Pratik', 'Adesh', 'Rohit']; console.log(fruits.length); // Output: 4
Aray Slice() - The
slice()
method in JavaScript is used to extract a section of elements from an array and return them as a new array. It allows to specify the starting and ending indices of the desired portion to be sliced.const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const slicedNumbers = numbers.slice(2, 6); console.log(slicedNumbers); // Output: [3, 4, 5, 6]
Array toString() - The JavaScript method
toString()
converts an array to a string of (comma-separated) array values.const fruits = ['mango', 'apple', 'banana']; const fruitsString = fruits.toString(); console.log(fruitsString); // Output = "mango, apple, banaba"
in the above example, we have an array called
fruits
containing three elements by calling thetoString()
method on thefruits
array we convert it into a comma-separated string representation :"mango, apple, banana"
. ThetoString(0)
method converts each element of the array to a string and joins them together with a comma.Array push() -
push
is a method in JavaScript that allows adding one or more elements to the end of an array.// Creating an array with some fruits let fruits = ['apple', 'banana', 'orange']; // Now, let's add a new fruit to the end using push() fruits.push('grape'); // The 'grape' is added to the end of the array // The array now becomes ['apple', 'banana', 'orange', 'grape']
Array Concat() - the JavaScript
concat()
method is used to join or combine two or more arrays into a single array. It doesn't modify the original arrays but instead creates a new array containing all the elements from the original arrays.javascriptCopy codeconst array1 = [1, 2, 3]; const array2 = [4, 5, 6]; const newArray = array1.concat(array2); console.log(newArray); // Output: [1, 2, 3, 4, 5, 6]
In this example, we have two arrays,
array1
andarray2
. By callingconcat()
onarray1
and passingarray2
as an argument, we create a new array callednewArray
that contains all the elements fromarray1
followed by all the elements fromarray2
.The original arrays,
array1
andarray2
, remain unchanged. Theconcat()
method simply returns a new array that combines the elements of the original arrays.Array pop() - the
pop()
method is used to remove the last element from an array and return that element. It's like taking the last item out of a stack of items.Imagine you have a stack of books, with the newest book on top. When you use
pop()
on the stack, it removes the top book and gives it to you. So you can read it or do whatever you want with it.Similarly, with an array in JavaScript, when you use
pop()
, it removes the last element from the array and gives it back to you. The array becomes shorter by one element.javascriptCopy codelet fruits = ['apple', 'banana', 'orange']; let lastFruit = fruits.pop(); console.log(fruits); // Output: ['apple', 'banana'] console.log(lastFruit); // Output: 'orange'
In this example,
pop()
is used on thefruits
array. It removes the last element, which is'orange'
, and returns it. The modified array now contains['apple', 'banana']
. The returned value,'orange'
, is stored in the variablelastFruit
, which can be used later in the code.Remember,
pop()
only removes the last element of the array, so if you want to remove elements from the beginning or middle of the array, you'll need to use other methods or techniques.