An array is a data structure that contains a list of elements which store multiple values under a single variable.
let ListItems=; ListItems=['shoes','watch','bag'];
You can also declare it as:
myArray = Date.now; myArray = myFunction; myArray = myItems;
The purpose of using an array is to store** multiple values** in a single entity of a declared variable. Arrays are used when we want to access elements in an orderly fashion using a single variable. One can store strings, boolean and numbers in a single array.
let listItems = ['bag','shoes','dress']; console.log(listItems.push('watch'));
Push() doest not return the value that has been added to the array. It only returns the new length of the array. pop() – *The pop() method is used to *remove the last element from an array. It returns the value that has been popped out. Example:
let listItems = ['bag','shoes','dress']; console.log(listItems.pop());
Pop() returns the value that has been removed and not the array length like Push(). shift() – Shifting is similar to popping, working on the first element instead of the last. The shift() method is used to remove the first array element and shifts all other elements to a lower index. It will return you the string that has been shifted out. Example:
let listItems = ['bag','shoes','dress']; console.log(listItems.shift());
Shift() works same as pop() but it returns the first element of the array instead of the last one. unshift() – The unshift() method adds a new element at the beginning of an array and unshifts older elements. It is similar to Push() and returns the new array length. *Example: *
let listItems = ['bag','shoes','dress','watch']; console.log(listItems.unshift('phone'));
Unshift() will add the new element into the array and return the length of the new array. concat() – The concat() method creates a new array by concatenating or merging existing arrays. It does not modify the existing array and always returns a new array. Example:
let arr1 = ['red','blue','green']; let arr2 = ['colors','spraypaint', 'brush']; let newArr = arr1.concat(arr2); console.log(newArr);
toString() – The toString() method is used to convert an array to a string of array values, separated by commas. Example:
let colors = ['red','blue','green']; console.log(colors.toString());
join() – The join() method works same as toString(). It is used to join all array elements into a string, but in addition, you can specify the separator. Example:
let colors = ['red','blue','green']; console.log(colors.join("+"));
reverse() – The reverse() method is used to reverse the order of the elements in an array. It will change the original array and swap the order of the elements. Example:
let fruits = ['mango','apple','grapes']; console.log(fruits.reverse());
sort() – The sort() method is used to sort an array alphabetically. This function sorts the values as string by default. Example:
let fruits = ['mango','apple','grapes']; console.log(fruits.sort());
slice() – The slice() method is used to slice out a piece of an array into a new array. It creates a new array without removing any elements from the source array. It will return the value that has been sliced out from the array. Example:
let colors = ['red','blue','green','yellow','orange']; console.log(colors.slice(1,3));
Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.
Contact DataPierce for any questions or queries you have about website development, designing, marketing projects or any small or enterprise software development.
Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.
Do you want excellent and world class web development services for your valuable projects? Contact **RB Genie **now, we have more than 8 years experienced team of web developers, which specializes in overall web design and website development...