JavaScript Array | Array length, push, pop, shift, unshift, IndexOf, forEach

By | June 3, 2020

The topic contains a brief explanation of JavaScript Array and how to create and manipulate JavaScript arrays.

What is an Array

Arrays are the variables that are used to store multiple values in a single variable. This also can be compared with a variable that stores only one variable.
JavaScript arrays can capable to store multiple types of values, including number, string, objects, function, and even other arrays. This makes a more complex data structure like an array of arrays and an array of objects.

JavaScript Array

Create a JavaScript Array

Access an Array Item

On the time accessing an array from a variable we use an array index number. On JavaScript array operation, if you want the last element of an array, use ArrayName.length function and subtract 1 for the last array element.

Add an element at the End of an Array

For adding new array element at the end of an array we use the array push function. Array push function is useful for adding an element to end of an array.

Remove last element in Array

For removing operation in an array element, we use an array pop function. Array pop function removes an array element from last of an array.

Remove from the front of an Array

Using array shift function to remove first element of an array.

Adding the first element to an Array

For adding the first index element in an array we use array unshift function. Check below array unshift function uses the example :

Find the index of an Array Element

For finding the index of an array element we use array indexOf function with array element or value of array parameter. Check the below example for explanation.

Loop in an Array

For getting all elements at once we use a loop in an array. In JavaScript array, we use the forEach loop. In the below example, we print an array element name and its index.

Also Check:

Know more:

https://www.php.net/manual/en/function.array.php

Happy Coding..!

Share and Enjoy !

0Shares
0 0

Subscribe For Latest Updates

Sign up to best of PHP Code Solutions, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.

4 thoughts on “JavaScript Array | Array length, push, pop, shift, unshift, IndexOf, forEach

  1. Pingback: PHP Foreach, For and While Loops - PHPCODER.TECH

  2. Pingback: DOM | Document Object Model - PHPCODER.TECH

  3. Pingback: How to Convert JSON to Array PHP - PHPCODER.TECH

  4. Pingback: How PHP Code Works - PHPCODER.TECH

Leave a Reply

Your email address will not be published. Required fields are marked *