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] Arrays


# JavaScript Arrays


● Array

 An array is a special variable, which can hold more than one value at a time.
 An array can hold many values under a single name, and user can access the values by referring to an index number.

● Creating an Array

 Using an array literal is the easiest way to create a JavaScript Array.

● Using the JavaScript Keyword new

 The following example also creates an Array, and assigns values to it.

● Access the Elements of an Array

 I refer to an array element by referring to the index number.

● Access the Full Array

 with JavaScript, the full array can be accessed by referring to the array name.

● Arrays are Objects

 Arrays are a special type of objects. The typeof operator in JSP returns "object" for arrays. But, JSP arrays are best described as arrays.
Arrays use numbers to access its "elements". 

● Array Elements Can Be Objects

 JavaScript variables can be objects. Array are special kinds of objects.
Because of this, I can have functions in an Array. 

● Array Properties and Methods

 The real strength of JSP arrays are the built-in array properties and methods.

● The length Property

 The length property of an array returns the length of an array.

● Looping Array Elements

 The best way to loop through an array, is using a "for" loop.

● Adding Array Elements

 The easiest way to add a new element to an array is using the push method.


New element can also be added to an array using the length property.

● Associative Arrays

 Many programming languages support arrays with named indexes.
Arrays with named indexes are called associative arrays.
JavaScript does not support arrays with named indexes.
In JSP, arrays always use numbered indexes.

● The Difference between Arrays and Objects

 In JSP, Arrays use numbered indexes, but Objects use named Indexes.

● When to use Arrays / Objects

  • JavaScript does not support associative arrays.
  • You should use objects when you want the element names to be strings.
  • You should use arrays when you want the element names to be numbers.

● Avoid new Array()

 There is no need to use the JSP built-in array constructor new Array().
 Use [].

● How to Recognize an Array

 A common question is "How do I know if a variable is an array?"

Solution1

To solve this problem ECMAScript 5 defines a new method Array.isArray().


The problem with this solution is that ECMAScript 5 is not supported in older browsers.

Solution2

To solve this problem, my own isArray() function.

Solution3

The instanceof operator returns true if an object is created by a given constructor.


QuickEdit
Unknown
0 Comments
Share This Post :

You Might Also Like

No comments:

Post a Comment

Follow @SunriseSunsetBlog