JavaScript Arrays
Example:
If you have a list of items (a list of chocalate names, for example), storing the chocalate in single variables could look like this:
let choc1 = "mango"
let choc2 = "vanila"
let choc3 ="blueberry"
An array can hold many values under a single name, and you can access the values by referring to an index number.
1. Create Array using Literal
Creating an array using array literal involves using square brackets [] to define and initialize the array.
let a = [];
console.log(a);
// Creating an Array and Initializing with Values
let b = [10, 20, 30];
console.log(b);
Output
[]
[ 10, 20, 30 ]2. Create using new Keyword (Constructor)
The "Array Constructor" refers to a method of creating arrays by invoking the Array constructor function.
[ 10, 20, 30 ]
Basic Operations on JavaScript Arrays
1. Accessing Elements of an Array
Any element in the array can be accessed using the index number. The index in the arrays starts with 0.
let a = ["HTML", "CSS", "JS"];
// Accessing Array Elements
console.log(a[0]);
console.log(a[1]);
Output
HTML CSS2. Accessing the First Element of an Array
The array indexing starts from 0, so we can access first element of array using the index number.
Example-
let a = ["HTML", "CSS", "JS"];
// Accessing First Array Elements
let fst = a[0];
console.log("First Item: ", fst);
Output
First Item: HTML3. Accessing the Last Element of an Array
We can access the last array element using [array.length - 1] index number.
let a = ["HTML", "CSS", "JS"];
// Accessing Last Array Elements
let lst = a[a.length - 1];
console.log("First Item: ", lst);
Output
First Item: JS4. Modifying the Array Elements
Elements in an array can be modified by assigning a new value to their corresponding index.
let a = ["HTML", "CSS", "JS"];
console.log(a);
a[1]= "Bootstrap";
console.log(a);
Output
[ 'HTML', 'CSS', 'JS' ] [ 'HTML', 'Bootstrap', 'JS' ]5. Adding Elements to the Array
Elements can be added to the array using methods like push() and unshift().
- The push() method add the element to the end of the array.
- The unshift() method add the element to the starting of the array.
[ 'Web Development', 'HTML', 'CSS', 'JS', 'Node.js' ]
6. Removing Elements from an Array
Original Array: HTML,CSS,JS After Removing the last: HTML,CSS After Removing the First: CSS After Removing 2 elements starting from index 1: CSS
7. Array Length
We can get the length of the array using the array length property.
let a = ["HTML", "CSS", "JS"];
let len = a.length;
console.log("Array Length: " + len);
Output
Array Length: 38. Array Concatenation
Combine two or more arrays using the concat() method. It returns new array containing joined arrays elements.
let a = ["HTML", "CSS", "JS", "React"];let b = ["Node.js", "Expess.js"];// Concatenate both arrayslet concateArray = a.concat(b);console.log("Concatenated Array: ", concateArray);OutputConcatenated Array: [ 'HTML', 'CSS', 'JS', 'React', 'Node.js', 'Expess.js' ]9. Conversion of an Array to String
We have a builtin method tostring() to converts an array to a string.
let a = ["HTML", "CSS", "JS"];
// Convert array ot String
console.log(a.toString());
Output
HTML,CSS,JS10.Check the Type of an Arrays
The JavaScript typeof operator is used ot check the type of an array. It returns "object" for arrays.
let a = ["HTML", "CSS", "JS"]; // Check type of array console.log(typeof a)Outputobject

0 Comments