JavaScript Arrays | Basics | Events
An array is an ordered collection of homogeneous elemenets stored in the meory in contiguous locations. All the elements in a Javasritp array should be of the same type. Javascript does not support asociative arrys i.e., arrays containing keys and their rerspective values
To create an array, use
let arr=new Array();
To set a value for
arr[0] = "Gaurav";
arr[1]="Shirodkar"
;
To access an array value, use
arr[0];
Al;l the elements of the array are stroed in contiguous memory locations and their data type of a elements should be the same.
Suppose, I had an array called arr
let arr = [0,1,2,3,4,5];
If we want to erxtract a part of an array from 1 to 3, use
arr.slice(1,3);
To extract vfronm the end of the array,
use the '-' sign, for eg, the slice(-2,2)
To insert an eleemnt at tyhe of an array,
use arr.push(9);
The result will be :-
arr=[0,1,2,3,4,5,9];
To remove an element from the end of an array, use arr.pop();
The result will be :
arr=[0,1,2,3,4,5];
(Since 9 is removed, it will be removed from the array)
To split a string into differnet variables, use str.split(",");
Suppose, str = "1,2,3,4,5";
then str.split(",");
will return as arr (1,2,3,4,5)
To join array elements into a single string, use
arr.join(",");
Suppose arr=[1,2,3,4,5];
then arr.join(",") will be "1,2,3,4,5";
Comments
Post a Comment