MENU Follow
Maneesh Mogallapu's
blog
Discover JavaScript
Arrays & How to Use
Them
Maneesh Mogallapu
Aug 28, 2022 · 4 min read
TABLE OF CONTENTS
What is an Array?
Why to use Array?
Access the Elements of an Array
How many elements we can stor…
Array Operations / Methods
What is an Array?
An array is a special variable, which can
hold more than one value at a time.
Syntex:
COPY
const animals = [' ' , ' ' , ' ']
You can create or even identify an
array with " [ ] " brackets.
divide the values in an array using " , "
.
Let understand it better
COPY
const animals = [' ' , ' ' , ' '];
the indexing starts from 0 in an Array.
This how an Array holds the elements.
Why to use Array?
If we have many items so instead of
declaring each item separately, we can
declare them in array.
Access the Elements of an Array
we can access elements by index number
as below.
COPY
const animals = [' ' , ' ' , ' '
console.log(animals[0]);
Output:-
How many elements we can
store in array?
We can store N number of elements for
finding the length of an array we have
length property.
Length Property GH The length
property of an array return the length
of an array (the number of array
elements)
COPY
const animals = [' ' , ' ' ,
console.log(animals.length);
OutputGH 3
Array Operations / Methods
when you work with arrays , it is easy to
remove elements and add new elements.
Removing Elements
Pop Method GH The pop() method
removes last element from the array.
COPY
const animals = [' ' , ' ' ,
animals.pop();
console.log(animals)
Output:- [' ',' ']
Shift Method GH The shift method
removes the first array element and
"shifts" all the other elements to the
lower index.
COPY
const animals = [' ' , ' ' ,
animals.shift();
console.log(animals)
Output:- [' ,' ']
Adding Elements
Push Method GH The push method
adds anew element to an array at the
end.
COPY
const animals = [' ' , ' ' ,
animals.push(' ');
console.log(animals)
Output:- [' ',' ',' ',' ']
Unshift Method GH The unshift()
method adds a new element to an
array at the beginning, and "unshifts "
the older elements.
COPY
const animals = [' ' , ' ' ,
animals.unshift(' ');
console.log(animals)
Output:- [' ',' ',' ',' ']
Iterating Through Elements
ForEach Method GH The forEach()
method calls a function once for every
element in order. Syntex:-
COPY
array.forEach(function(currentVal
currentValue (requried) - the value
of an array;
index (optional) - the index of the
current element;
arr (optional) - the array of the
current elements
COPY
const animals = [' ' , ' ' ,
animals.forEach(function(value, i
console.log(index+"-"+value);
});
OutputGH 0H 1H 2H
Some Method GH The some () method
checks whether at least one of the
elements of the array satisfies the
condition checked by the argument
method.
COPY
const animals = [' ' , ' ' ,
//checks whether an element we ar
const Test = (element) => element
console.log(animals.some(Test));
Output:- true
Combining Arrays
Concat Method GH The concat()
method is used to join two or more
arrays.
This method does not change the
existing arrays, but return a new
array, containing the values of the
joined array.
COPY
const animals = [' ' , ' ' ,
const birds = [' ' , ' ' , ' '
const result=animals.concat(birds
console.log(result);
Output:- [' ',' ',' ',' ',' ',
' ']
Filtering Arrays
Filter Method GH The filter() method
creates a new array with all elements
that pass the test implemented by the
provided function.
COPY
const animals = [' ' , ' ' ,
const result = animals.filter(ani
console.log(result);
Output:-[' ']
Reordering Arrays
Sort Method GH The sort() method
sorts the elements of an array and
returns the reference to the same
array, now sorted. The default sort
order is ascending.
COPY
const animals = ["cat " , "hamster
animals.sort()
console.log(animals);
Output:-["cat " , "dog " , "hamster
"]
Reverse Method GHThe reverse()
method reverses an array and returns
the reference to the same array,
elements order in the array will be
turned towards the direction opposite
to that previously stated.
COPY
const animals = ["cat " , "hams
animals.reverse()
console.log(animals);
Output:-["dog " , "hamster ",
"cat "]
Modifying Arrays
Map Method GH The map() method
creates a new array populated with
the results of calling a provided
function on every element in the
calling array.
COPY
const animals = [' ' , ' ' ,
const result = animals.map(animal
console.log(result);
Output:-['true' , 'false' , 'false']
JavaScript #learncodeonline
javascript array javascript array methods
ineuron
1 1
Comments
Write a comment
©2022 Maneesh Mogallapu's blog
Archive · Privacy policy · Terms
Publish with Hashnode
Powered by Hashnode - a blogging community for
software developers.