Array Properties and Common Methods Overview

length

var arr1 = [1, 2, 3, 4, 5]; // Assign five elements to the array
var arr2 = new Array(100); // Allocate space of size 100 for the array
var arr3 = new Array(1, 2); // Assign two elements to the third array
console.log(`Length of Array 1:`, arr1.length); // 5
console.log(`Length of Array 2:`, arr2.length); // 100
console.log(`Length of Array 3:`, arr3.length); // 2

Common Array Methods

toString()

var arr = [1, 2, 3, 4];
console.log('arr:', arr, 'type of arr:', typeof arr);
var str_arr = arr.toString();
console.log('type of arr:', typeof arr, 'type of str_arr:', typeof str_arr);
console.log('arr:', arr, 'str_arr:', str_arr);

Output:

arr: [ 1, 2, 3, 4 ] type of arr: object
type of arr: object type of str_arr: string
arr: [ 1, 2, 3, 4 ] str_arr: 1,2,3,4

forEach()

The forEach() method can be used to execute a callback function for each element in the array.
The forEach() method accepts a callback function as its argument, which is invoked once for every element during iteration over the array. The callback function can accept three parameters: the current element being iterated, the index of that element, and the entire array itself. For example:

const arr = ['foo', 'bar', 'baz'];
arr.forEach(function(element, index, array) {
  console.log(element, index, array);
});

The above code will output:

foo 0 [ 'foo', 'bar', 'baz' ]
bar 1 [ 'foo', 'bar', 'baz' ]
baz 2 [ 'foo', 'bar', 'baz' ]

Using the forEach() method to iterate through elements in an array and perform operations on each element, such as modifying its value or passing it to another function for processing.