Page 238 - Web_Application_v2.0_C12_Fb
P. 238
Do you know?
The respective array element is accessed by enclosing the index value in square brackets.
2.12 ARRAY PROPERTIES AND METHODS
JavaScript arrays have various built-in properties and methods. Some of those are:
Length Property
The length property of an array returns the number of array elements. For example:
const fruits = ["pineapple", "pear", "jackfruit", "strawberry"];
var lgth = fruits.length; //This will return 4.
Array Methods
To work with arrays easily, JavaScript provides many built-in methods (special functions) that help you
manipulate and manage array data.
Here, the sample to perform array methods is:
Fruits = ['Apple', 'Mango', 'Banana', 'Papaya'];
Some methods of array are as follows:
Methods Description Usage Output
toString() Converts an array into a string of the Fruits.toString(); Apple,Mango, Banana,Papaya
array values separated by commas.
pop() Removes the last element in an array. Fruits.pop(); Apple,Mango,Banana
push() Adds a new element at the end of the Fruits.push('Orange'); Apple, Mango,Banan, Papaya, Orange
array.
shift() Removes the first member from the Fruits.shift(); Mango, Banana, Papaya
array and "shifts" every subsequent
element to a lower index.
unshift() Adds a new element to an array (at Fruits.unshift("Grapes"); Grapes, Apple, Mango, Banana,
the beginning), and "shifts" all other Papaya
elements to a higher index.
join() Join all array elements into a string. Fruits.join('&'); tApple&Mango&Banana&Papaya
concat() Creates a new array by merging Fruits.concat(moreFruits); Applex, Mango, Banana, Papaya,
(concatenating) existing arrays. Orange, Kiwi
slice() Gives back a new array that has a copy Fruits.slice(1, 3); Mango, Banana
of the given array's part.
reverse() Changes the original array by reversing Fruits.reverse(); Papaya, Banana, Mango, Apple
the order of the elements.
sort() Sorts a string array in ascending order Fruits.sort(); Apple, Banana, Mango, Papaya
alphabetically.
The detailed explanation of these methods is as follows:
1. The toString() Method: The toString() method converts an array into a string, with the values
separated by commas. It does not modify the original array, but instead returns a string representation of
the array.
236 Touchpad Web Applications (Ver. 2.0)-XII

