Javascript Array Variable Types

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/javascript-array-variable-types/
PINTEREST
PINTEREST
INSTAGRAM

While programming we generally need to use multiple even several hundred variables of single type. Should we define one variable for each value? No. We can use arrays which can hold multiple values in a single variable. An array is string of values. So it gives the flexibility to store more than one data into single variable. An array can be defined like below.

  • var tag variable
  • names is array
  • new Array creates new array with specified values. Each value have to separated

  • This is simpler definition of the array there is no difference.
  • [ ] is used to surround array elements

Arrays element count can not be more than 4,294,967,295.

Index

As we have learned that arrays have multiple elements. Index is used to specify only one element in an array.

  • We want to get berry and use [ ] to specify index 1.
  • Arrays indexes starts from 0

Get Length of Array

Arrays size can be changed while using the by adding or removing elements. Array size can be get with length

  • length gets the length of the array

  • We have three elements

 Joining Two Arrays

Two arrays can be joined together with concat method of array.

  • Fruits array concat function is used to join numbers array.
  • Joined is new array consist of fruits and numbers
LEARN MORE  How To Loop and Iterate Over Python Dictionary?

Find Element Index of Array

We can get the index of element with indexOf function. We will provide the element value.

  • We use indexOf function and provide “berry” as element. This will return

  • The result of the indexOf will be 1

Add Element To Array

Adding element to an array can be done with push function.

  • Melon is added to the fruits array with push function

  • This is our final fruits array

Remove Element From Array

Removing element from array is done with pop function. Pop function will return the last element of the array by removing. Keep in mind that this will return last element as a result too.

  • pop function removes lemon from array and returns it
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/javascript-array-variable-types/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)