Theme Layout

Boxed or Wide or Framed

Wide

Theme Translation

Display Featured Slider

Featured Slider Styles

Display Grid Slider

yes

Grid Slider Styles

Display Trending Posts

Display Author Bio

Display Instagram Footer

off

Dark or Light Style

Light

Blog Archive

Powered by Blogger.

Thursday, February 16, 2017

[JavaScript] Array Methods


# JavaScript Array Methods


● Converting Arrays to Strings

The JavaScript method toString() converts an array to a string of array values.


The join() method also joins all array elements into a string.
It behaves just like toString(), but in addition I can specify the separator.

● Popping and Pushing

when I work with arrays, it is easy to remove elements and add new elements.
This is what popping and pushing is.
Popping items out of an array, Pushing items into an array.

● Popping and Pushing

The pop() method removes the last element from an array.


The pop() method returns the value that was "popped out".

● Pushing

The push() method adds a new element to an array.


The push() method returns the new array length.

● Shifting Elements

shifting is equivalent to popping, working on the first element instead of the last.
The shift() method removes the first array element and "shifts" all other elements to a lower index.


The shift() method returns the string that was "shifted out".


the unshift() method adds a anew element to an array, and "unshifts" older elements.


the unshift() method returns the new array length.

● Changing Elements

Array elements are accessed using their index number.


The length property provides an easy way to append a new element to an array.

● Deleting Elements

Since JavaScript arrays are objects, elements can be deleted by using the JSP operator delete.

● Splicing an Array

The splice() method can be used to add new items to an array.


The 1st parameter (2) defines the position where new elements should be added.
The second parameter (0) defines how many elements should be removed.
the rest of the parameters ("chn","afr") define the new elements to be added.

● Using splice() to Remove Elements

With clever parameter setting, I can use splice() to remove elements without leaving "holes" in the array.


The first parameter defines the position where new elements should be added.
The second parameter defines how many elements should be removed.
The rest of the parameters are omitted. No new elements will be added.

● Joining Arrays

The concat() method creates a new array by concatenating two arrays.


The concat() method can take any number of array arguments.

● Slicing an Array

The slice() method slices out a piece of an array into a new array.
this example slices out a part of an array starting from array element1.


The slice() method can take two arguments like slice(1,3).
The method then selects elements from the start argument, and up to the end argument.


If the end argument is omitted, like in the first examples, the slice() method slices out the rest of the array.

● Automatic toString()

JavaScript will automatically convert an array to a string when a primitive value is expected. These two examples will produce the same result.


Above code is same as below code.


QuickEdit
Unknown
0 Comments
Share This Post :

You Might Also Like

No comments:

Post a Comment

Follow @SunriseSunsetBlog