DEV Community

G Gokul
G Gokul

Posted on

ARRAYS IN JAVASCRIPT

Arrays in js:

  • An Array is an object type designed for storing data collections.
  • An array is a list of values, known as elements.
  • Array elements are ordered based on their index.
  • The first element is at index 0, the second at index 1, and so on.
  • Arrays can grow or shrink as elements are added or removed.

array

  • Arrays can store elements of different data types (numbers, strings, objects and other arrays).

1. Create Array using Literal:

Creating an array using array literal involves using square brackets [] to define and initialize the array.

example:
let b = [10, 20, 30];
console.log(b);

output:
[10, 20, 30]

2. Create using new Keyword (Constructor):(TBD)

The "Array Constructor" refers to a method of creating arrays by invoking the Array constructor function.

example:
let a = new Array(10, 20, 30);
console.log(a);

output:
[10, 20, 30]

Basic Operations on JavaScript Arrays:

(TBD)

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.

example:
let k = ["HTML", "CSS", "JS"];
console.log(k[0]);
console.log(k[1]);

output:
html
css

2. Modifying the Array Elements:

  • Elements in an array can be modified by assigning a new value to their corresponding index.

example:
let g = ["HTML", "CSS", "JS"];
console.log(g);
g[1] = "Bootstrap";
console.log(g);

output:
['HTML', 'CSS', 'JS']
['HTML', 'Bootstrap', 'JS']

3. 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"]

4. 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']

5. 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

6. 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)