jQuery Array | declare, print and functions of arrays in jQuery with examples

What is Array

An array is a type of variable that can store multiple values.

The array is a data structure that can hold one or more values in a single variable.

See an array example

Let us say, we want to store 50 US States in our jQuery program in a variable. What we can do is to create 50 simple variables:

var myvar1;

var myvar2;

And so on.

On the other hand, if you make that variable (myvar) an array in jQuery, you can store all 50 state names in a single variable.

How to declare an Array in jQuery

There are two ways to declare jQuery array:

An array with constructor

var State_Array = new Array( “NY”, “NJ” );

An array with literal declaration

var State_Array = [ “NY”, “NJ” ];

The Literal declaration is a preferred method to declare arrays in jQuery/JavaScript.

Basic Example of declaring and printing an array

Following example shows how to create a jQuery array (or JavaScript).

Experience this example online



A few array functions and properties

Following are a few functions and a property of the array.

.length property

The length property of array jQuery is used to get the number of items in an array.

An example of using array length:

Experience this example online



You may need .length property to be used in For loops.

.reverse

It reverses the order of array elements.

 

Also see .forEach


Was this article helpful?

Related Articles

Leave A Comment?