Open In App

How to use array with jQuery ?

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An array is a linear data structure. In JavaScript, arrays are mutable lists with a few built-in methods, we can define arrays using the array literal. The arrays can be used in the same way in jQuery as used in JavaScript.

Syntax And Declaration:

let arr1=[];
let arr2=[1,2,3];
let arr2=["India","usa","uk"];

NOTE: The type of an array in JavaScript and all its frameworks or libraries will always be an object.

Javascript




const jQueryArr = [1, 2, 3, "GFG", "jQuery"];
console.log(jQueryArr, typeof jQueryArr);


Output

[ 1, 2, 3, 'GFG', 'jQuery' ] object

Iterating through an array in jQuery

jQuery provides a generic .each() method to iterate over elements of arrays, as well as properties of objects. In the case of an array, the callback is passed an array index and a corresponding array value each time. The method returns its first argument, the object that was iterated.

Example: The below example will explain how you can iterate through an array in jQuery using .each() method.

Javascript




const arr = ["hello","from","GFG"];
 
// Traversing using jQuery method
console.log("traversing in array using jQuery");
jquery.each(arr, function(index,value) {
    console.log('index: ' + index + '   ' + 'value: ' + value);
});


 Output:

index: 0 value: hello
index: 1 value: from
index: 2 value: GFG

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads