ARRAY:
- An Array is an object type designed for storing data collections.
- Key characteristics of JavaScript arrays are:
Elements: An array is a list of values, known as elements.
Ordered: Array elements are ordered based on their index.
Zero indexed: The first element is at index 0, the second at index 1, and so on.
Dynamic size: Arrays can grow or shrink as elements are added or removed.
Heterogeneous: Arrays can store elements of different data types (numbers, strings, objects and other arrays).
REF: W3 school
Array indexing:
Syntax of Array:
- 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.
To get a single element from the list of an array:
Example:
Output: 49
2. Modifying the Array Elements:
- Elements in an array can be modified by assigning a new value to their corresponding index. example:

output:
['HTML', 'CSS', 'JS']
['HTML', 'Bootstrap', 'JS']
- 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.
example:
let a = ["HTML", "CSS", "JS"];
a.push("Node.js");
a.unshift("Web Development");
console.log(a);
output:
["Web Development","HTML","CSS","JS","Node.js"]
- Removing Elements from an Array:(TBD)
To remove the elements from an array we have different methods like pop(), shift(), or splice().
The pop() method removes an element from the last index of the array.
The shift() method removes the element from the first index of the array.
The splice() method removes or replaces the element from the array.
example:
// Creating an Array and Initializing with Values
let m = ["HTML", "CSS", "JS"];
console.log(m);
// Removes and returns the last element
let lst = m.pop();
console.log(m);
// Removes and returns the first element
let fst = m.shift();
console.log(m);
// Removes 2 elements starting from index 1
m.splice(1, 2);
console.log(m);
output:
['HTML', 'CSS', 'JS']
['HTML', 'CSS']
['CSS']
['CSS']
- Array Length:
We can get the length of the array using the array length property.
example:
// Creating an Array and Initializing with Values
let a = ["HTML", "CSS", "JS"];
let len = a.length;
console.log("Array Length: " + len);
output:
Array Length: 3
- Iterating Through Array Elements:
example:
let z = ["HTML", "CSS", "JS"];
for (let i = 0; i < z.length; i++) {
console.log(z[i])
}
output:
html
css
js


Top comments (0)