What is array ?
- In JavaScript, an array is an ordered list of values. Each value, known as an element, is assigned a numeric position in the array called its index.
- The indexing starts at 0, so the first element is at position 0, the second at position 1, and so on.
- Arrays can hold any type of data-such as numbers, strings, objects, or even other arrays-making them a flexible and essential part of JavaScript programming.
// Creating array
let numbers = [2, 4 , 8, 12, 16];
//Printing array
console.log(numbers);
Why Use Arrays?
We use an array to store multiple values in a single variable.
Without an array:
let mark1 = 80;
let mark2 = 90;
let mark3 = 75;
With an array:
let marks = [80, 90, 75];
Array Methods
JavaScript allows us to do such operations as adding, removing, updating, or getting elements, transforming, or iterating through arrays.
Array length
Array toString()
Array pop()
Array push()
Array shift()
Array unshift()
Array length
The length property returns the length (size) of an array:
let a = ["HTML", "CSS", "JS", "React"];
console.log(a.length);
output:
4
Array toString()
The toString() method converts the given value into the string with each element separated by commas.
let a = ["HTML", "CSS", "JS", "React"];
let s = a.toString();
console.log(s);
output:
HTML,CSS,JS,React
Array.pop() Method
The pop() method is used to remove elements from the end of an array.
let a = [20, 30, 40, 50];
a.pop();
console.log(a);
output:
[20,30,40]
Array.push() Method
The push() method is used to add an element at the end of an Array.
let a = [10, 20, 30, 40, 50];
a.push(60);
console.log(a);
otupt:
[10,20,30,40,50,60]
Array.shift() Method
The shift() method is used to remove elements from the beginning of an array
let a = [20, 30, 40, 50];
a.shift();
console.log(a);
output:
[30,40,50]
Array.unshift() Method
The unshift() method is used to add elements to the front of an Array.
let a = [20, 30, 40];
a.unshift(10);
console.log(a);
output:
[10, 20, 30, 40]
Array Iterations
Array forEach()
Array map()
Array filter()
Array forEach()
The forEach() method calls a function (a callback function) for each array element.
const num=[1,2,3,4,5,6,7,8,9,10];
function number(value)
{
console.log(value);
}
console.log(num);
num.forEach(number)
output:
[
1, 2, 3, 4, 5,
6, 7, 8, 9, 10
]
1
2
3
4
5
6
7
8
9
10
Array map()
- The map() method creates a new array by performing a function on each array element.
- The map() method does not execute the function for array elements without values.
- The map() method does not change the original array.
const nums = [1, 2, 3];
function maps(value){
console.log(value);
return value * 5;
}
result=nums.map(maps);
console.log(result);
output:
1
2
3
[ 5, 10, 15 ]
Array filter()
The filter() method creates a new array with array elements that pass a test.
let numb = [5, 10, 15, 20];
let results = numb.filter(function(value) {
return value >= 10;
});
console.log(numb);
console.log(results);
output:
[ 5, 10, 15, 20 ]
[ 10, 15, 20 ]
Top comments (0)