The Wayback Machine - https://web.archive.org/web/20240619053241/https://www.geeksforgeeks.org/javascript-array-reference/
Open In App

JavaScript Array Reference

Last Updated : 16 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Array is used to store multiple elements in a single variable. It can hold various data types, including numbers, strings, objects, and even other arrays. It is often used when we want to store a list of elements and access them by a single variable.

Syntax:

const arr = ["Item1", "Item2", "Item3", ...];

Example: Creating an array and copying its items to another array, and displaying the copied items.

JavaScript




// Create and initialize an array
let courses = ["HTML", "CSS", "JavaScript", "React"];
  
// Display the array items
console.log(courses);
   
// Create a new empty array
let newArray = [];
   
// forEach loop to push elements
// into new array
courses.forEach(function (course) {
    newArray.push(course);
});
   
// Display the new array of items
console.log(newArray);


Output

[ 'HTML', 'CSS', 'JavaScript', 'React' ]
[ 'HTML', 'CSS', 'JavaScript', 'React' ]

JavaScript Array Constructor

In JavaScript, a constructor gets called when an object is created using the new keyword.

Constructor

Descriptions

Examples

Array() It is used to create an array.
Try

JavaScript Array Properties

A JavaScript property is a member of an object that associates a key with a value.

Instance Property:

An instance property is a property that has a new copy for every new instance of the class.

Instance Properties

Descriptions

Examples

constructor Returns only reference of the function and does not return the name of the function.
Try

length Set or return the number of elements in an array.
Try

JavaScript Array Methods

JavaScript methods are actions that can be performed on objects.

Static Method:

If the method is called using the array class itself then it is called a static method.

Static Methods

Descriptions

Examples

from() Returns an array object from any object with a length property or an iterable object.
Try

isArray() Returns true if the argument passed is an array else it returns false.
Try

of() Creates a new array instance with variables present as the argument of the function.
Try

Instance Method:

If the method is called on an instance of a array then it is called an instance method.

Instance Methods

Descriptions

Examples

at() Returns the element of that index
Try

concat() Merge two or more arrays together.
Try

copyWithin() Copies part of an array to the same array itself and returns.
Try

entries() Fetch all the entries of the same data structure.
Try

every() checks that whether all the elements of the array satisfy the given condition.
Try

fill() Fill the array with a given static value.
Try

filter() Builds a new array containing elements that satisfy a function’s test.
Try

find() Get the value of the first element in the array that satisfies the provided condition.
Try

findIndex() Return the first index of the element in a given array that satisfies the provided testing function.
Try

flat() Flatten an array, to reduce the nesting of an array.
Try

flatMap() This is used to flatten the input array element into a new array.
Try

forEach() It is provided a function once for each element of the array.
Try

includes() If an array contains the certain value, it returns true.
Try

indexOf() Return the first index at which a given element may be found, or -1 if it does not exist.
Try

join() Join the elements of an array into a string.
Try

keys() Return a new array iterator which contains the keys for each index in the given input array.
Try

lastIndexOf() Return the last index at which a given element may be found, or -1 if it does not exist.
Try

map() Calls the argument function once for each element of the given array in order.
Try

pop() Remove the last element of the array and also returns the removed element.
Try

push() Push one or more values into the array.
Try

reduce() Reduce the array to a single value and executes a provided function for each value of the array.
Try

reduceRight() Convert elements of the given array from right to left to a single value.
Try

reverse() This is used for the in-place reversal of the array.
Try

shift() Removes the first element of the array thus reducing the size of the original array by 1.
Try

slice() Returns a new array containing a portion of the array on which it is implemented.
Try

some() Each array element’s callback function is run once.
Try

sort() Sort an array in place in a given order according to the compare() function.
Try

splice() Modify the contents of an array by removing the existing elements.
Try

toLocaleString() Convert the elements of the given array to string.
Try

toString() Return the string representation of the array elements.
Try

unshift() Add one or more elements to the beginning of the given array.
Try

values() Return a new array Iterator object that contains the values for each index in the array.
Try



Previous Article
Next Article

Similar Reads

JavaScript is showing reference error "Prompt is not defined"
In this article, we cover the reference error that is "Prompt is not defined". It's important to understand the tools you are using. I know, this can be very overwhelming in the beginning. but here, we will solve this reference error with not only one solution but also make more ways to done properly. window.prompt() instructs the browser to displa
2 min read
JavaScript Math Reference
JavaScript Math object is used to perform mathematical operations on numbers. Math is an inbuilt object that works with numbers types but it does not work with BigInt. Example: Below example will give you a brief idea of JavaScript math objects. C/C++ Code // Return PI value(3.141592653589793) console.log(Math.PI); Output: This will print the value
4 min read
JavaScript Boolean Reference
JavaScript Boolean is a datatype that returns either true or false In JavaScript, a boolean is used as a function to get the value of a variable, object, conditions, expressions, etc in terms of true or false Syntax: Boolean(variable/expression) Example: If the operands are equal, the equal to operator == returns true C/C++ Code <script> cons
2 min read
JavaScript BigInt Reference
JavaScript BigInt is an inbuilt object in JavaScript that provides a way to represent whole numbers larger than 253-1 which is the largest number that can represent with the Number primitive. It is represented by the MAX_SAFE_INTEGER constant. Example: This example creates a BigInt by appending n at the end of the number. C/C++ Code // Decimal form
2 min read
Primitive and Reference value in JavaScript
In JavaScript, a variable may store two types of values, Primitive values or Reference values. This article will describe and help to compare both these types of values. Primitive value: JavaScript provides six types of primitive values that include Number, String, Boolean, Undefined, Symbol, and BigInt. The size of Primitive values are fixed, ther
2 min read
Reference and Copy Variables in JavaScript
In this article, we will talk about pass-by-value and pass-by-reference in JavaScript. JavaScript always passes by value, but in an array or object, the value is a reference to it, so you can 'change' the data. JavaScript has 5 primitive data types that are passed by value, they are Boolean, NULL, undefined, String, and Number. It has 3 non-primiti
5 min read
JavaScript ReferenceError - Reference to undefined property "x"
This JavaScript warning reference to undefined property occurs if a script tries to access an object property that doesn't exist. Message: ReferenceError: reference to undefined property "x" (Firefox) Error Type: ReferenceError(Only reported by firefox browser) Cause of the error: The script is trying to access an object property that doesn't exist
2 min read
Call by Value Vs Call by Reference in JavaScript
Call by Value: In this method, values of actual parameters are copied to the function’s formal parameters, and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in the actual parameters of the caller. Suppose there is a variable named “a”. Now, we store a primitive value(boo
3 min read
Foundation CSS Dropdown Menu JavaScript Reference
Foundation CSS is an open-source and responsive front-end framework built by the ZURB foundation in September 2011, that makes it easy to layout stunning responsive websites, apps, and emails that appear amazing and can be accessible to any device. The Dropdown Menu is used for displaying an expandable dropdown menu by using the Foundation CSS plug
6 min read
JavaScript Proxy/handler Reference
JavaScript Proxy Object is used to define the custom behavior of fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc). Syntax: const p = new Proxy(target, { Proxy method: function(target, thisArg, argumentsList) { } }); Example: Below examples illustrate the handler.apply() method in JavaScript: C/C++ Cod
2 min read