JavaScript Array Reference

📌 JavaScript Array Reference

Last Updated: 16 Jan, 2024

📌 What is a JavaScript Array?

A JavaScript array is used to store multiple values in a single variable. It can hold numbers, strings, objects, and even other arrays.

✅ Syntax:

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

✅ Example: Creating and Copying an Array


// Create an array
let courses = ["HTML", "CSS", "JavaScript", "React"]; 

// Display the array items
console.log(courses); 

// Create a new array and copy elements
let newArray = []; 
courses.forEach(course => newArray.push(course)); 

// Display the copied array
console.log(newArray);
    

🖥️ Output:

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

🛠️ JavaScript Array Constructor

The Array() constructor creates an array.

Constructor Description
Array() Creates an array.

📌 JavaScript Array Properties

Properties are characteristics of an array object.

Instance Property Description
constructor Returns the reference of the function that created the array.
length Returns or sets the number of elements in an array.

📌 JavaScript Array Methods

JavaScript methods perform actions on arrays.

✅ Static Methods

Method Description
Array.from() Creates an array from any object with a length property.
Array.isArray() Checks if the argument is an array.
Array.of() Creates a new array from arguments passed.

✅ Instance Methods

Method Description
at() Returns an element at a specific index.
concat() Merges two or more arrays.
copyWithin() Copies part of an array to another location.
entries() Returns an array iterator with key-value pairs.
every() Checks if all elements pass a test.
filter() Creates a new array with elements that pass a test.
find() Finds the first element that satisfies a condition.
flat() Flattens a nested array.
forEach() Executes a function for each array element.
includes() Checks if an array contains a specified value.
join() Joins all elements into a string.
map() Creates a new array by applying a function to each element.
pop() Removes the last element and returns it.
push() Adds elements to the end of an array.
reverse() Reverses the order of elements in an array.
sort() Sorts elements alphabetically or numerically.

🎯 Conclusion

JavaScript arrays are powerful for handling multiple values efficiently. Understanding their properties and methods will help you write optimized and clean code.

✨ Happy Coding! 🚀

Powered by Blogger.