DEV Community

Keerthana M
Keerthana M

Posted on

Array in JS

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:

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

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:


output:
["Web Development","HTML","CSS","JS","Node.js"]

4. Array Length:

  • We can get the length of the array using the array length property.

example:
// Creating an Array and Initializing with Values


output:
Array Length: 3

5. Iterating Through Array Elements:

example:


output:
html
css
js

Top comments (0)