0% found this document useful (0 votes)
1 views2 pages

In JavaScript, Arrays Are A Fundame

JavaScript arrays are essential for storing ordered collections of values and support various operations such as creation, modification, iteration, searching, and transformation. Core operations include adding/removing items with methods like push, pop, and splice, as well as iterating through elements using for loops or forEach. Additionally, transformation methods like map, filter, and reduce allow for creating new arrays based on the original array's data.

Uploaded by

devkotabhim83
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

In JavaScript, Arrays Are A Fundame

JavaScript arrays are essential for storing ordered collections of values and support various operations such as creation, modification, iteration, searching, and transformation. Core operations include adding/removing items with methods like push, pop, and splice, as well as iterating through elements using for loops or forEach. Additionally, transformation methods like map, filter, and reduce allow for creating new arrays based on the original array's data.

Uploaded by

devkotabhim83
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

In JavaScript, arrays are a fundamental data structure used to store ordered

collections of multiple values under a single variable name. You can perform a wide
range of operations on them, including creation, modification, iteration,
searching, and transformation.
Core Operations
Creation: Arrays are typically created using square brackets [], known as array
literals. You can also use the Array() constructor, though it is less common.
javascript
const fruits = ["apple", "banana", "mango"];
Accessing Elements: Elements are accessed using zero-based index numbers in square
brackets.
javascript
[Link](fruits[0]); // Outputs: "apple"
Modifying Elements: You can change an element's value by assigning a new value to
its index.
javascript
fruits[0] = "tahini"; // fruits is now ["tahini", "banana", "mango"]
Length Property: The .length property returns the number of elements in the array.
javascript
[Link]([Link]); // Outputs: 3

Modifying the Array Structure


JavaScript provides several built-in methods to add or remove elements from arrays.

Adding Items:
push(...items): Adds items to the end of the array.
unshift(...items): Adds items to the beginning of the array.
splice(pos, deleteCount, ...items): A versatile method that can add/remove elements
at a specific index (pos), removing deleteCount elements and inserting new items.
Removing Items:
pop(): Removes the last element from the array and returns it.
shift(): Removes the first element from the array and returns it.
splice(): Can also be used for removing items at any position.
Iterating and Transforming Arrays
You can loop through array elements or create new arrays based on operations
performed on the original.
Iteration:
for loops or while loops are standard methods for iterating using an index.
for...of loop: A modern, simpler syntax to iterate over the elements directly.
forEach(): Executes a provided function once for each array element.
Transformation/Filtering (return a new array):
map(func): Creates a new array with the results of calling a function on every
element.
filter(func): Creates a new array with only elements for which the provided
function returns true.
reduce(func, initial): Executes a reducer function on each element, resulting in a
single output value.
slice(start, end): Returns a shallow copy of a portion of an array into a new
array.
concat(...items): Merges two or more arrays into a new array.
reverse(): Reverses the order of elements in place within the original array.
sort(): Sorts the elements of an array in place.
Searching
indexOf(item) / lastIndexOf(item): Returns the first/last index at which a given
item is found, or -1 if not present.
includes(value): Returns a boolean (true or false) indicating whether the array
contains a certain value.
find(func): Returns the value of the first element that satisfies a testing
function, or undefined if none is found.
findIndex(func): Similar to find, but returns the index of the element.

You might also like