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

Js Arrays

Uploaded by

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

Js Arrays

Uploaded by

vikashgrg06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

JS

Arrays
Arrays
• An array is a special variable, which can hold
more than one value
• An array can hold many values under a single
name, and you can access the values by
referring to an index number.
• Syntax
• const array_name = [item1, item2, ...];
Examples
• const cars = [
"Saab",
"Volvo",
"BMW"
];
• const cars = [];
cars[0]= "Saab";
cars[1]= "Volvo";
cars[2]= "BMW";
• const cars = new Array("Saab", "Volvo", "BMW");
 There is no need to use new Array().
 For simplicity, readability and execution speed, use the array literal
method.
Index
• Array indexes start with 0.
• [0] is the first element. [1] is the second element.
• Example
• const cars = ["Saab", "Volvo", "BMW"];
let car = cars[0];
 0 -> Saab = car[0]
 1 -> volvo= car[1]
 2-> BMV=ca[2]
Changing Value
• To change element Value , we use index
property
• Example:
• car[0]=“Tata” , car[1]=“maruti”;
• To change the full array we can accessed by
referring to the array name
• Example
• document.write(cars);
Arrays are Objects
• Arrays are a special type of objects. The typeof operator
in JavaScript returns "object" for arrays.
• As JS arrays are heterogeneous.
• can have variables of different types in the same Array.
Also,can have functions in an Array as well as can have
arrays in an Array
• Example: const person = ["John", "Doe", 46];
• Different
• Arrays use numbers to access its "elements".
• Objects use names to access its "members".
The length Property

• The length property of an array returns the


length of an array (the number of array
elements).
• Example:
• cars.length // Returns the number of
elements
• The length property is always one more than
the highest array index. (cars[cars.length - 1])
Array Methods
• add a new element to an array is using
the push() method
• to remove elements from an array we use the
pop() method. It removes (pops) the last
element of an array.
• The sort() method sorts an array alphabetically.
(not numerically)
• The reverse() method reverses the elements in
an array.
• The forEach() method calls a function for each
element in an array.
• The forEach() method is not executed for
empty elements.
Map Method
• 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.
• .The filter() method creates a new array with
array elements that pass a test.
• The every() method checks if all array values
pass a test
Const Array
• It does NOT define a constant array. It defines
a constant reference to an array.
• Because of this, we can still change the
elements of a constant array
• Example
• const cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"]; // ERROR
• Cars[1] =“ABC” //Allowed
• const variables must be assigned a value when
they are declared
• An array declared with const has Block Scope.
• Thanks

You might also like