The length Update
An update in an array length modifies the array.
See the example below:
const fruits = [];
fruits[132] = "Apple";
console.log( fruits.length ); // 133 => last index + 1
console.log( fruits.length - 100 ); // 33
Writable length property
We can reduce the initial length of an array by the length
property.
See the example below:
const fruits = [ 'Apple', 'Orange', 'Melon' ];
fruits.length = 2
console.log( fruits[0] ); // Apple
console.log( fruits[1] ); // Orange
console.log( fruits[2] ); // Undefined
This means to clear an array you can use, arr.length = 0
new Array(length)
As shown in the previous article, the other syntax to create an array is :
array = new Array(elem1, elem2, ..., elemN)
See the example below:
const fruits = new Array('Apple', 'Orange', 'Melon');
console.log( fruits[0] ); // Apple
console.log( fruits[1] ); // Orange
When the constructor has only one argument as a number, the argument becomes the length of the object.
const arr = new Array(3);
console.log( arr[0] ); // undefined
console.log( arr[1] ); // undefined
console.log( arr[2] ); // undefined
console.log( arr.length ) // 3
Multidimensional Arrays
Arrays that are items to another array create 2 or more or multidimensional array. A good example is the storage of matrices.
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
alert( matrix[1][1] ); // 5
Arrays toString
An array can be converted to a string by the syntax shown below:
array.toString()
See the example below:
const arr = [ 1, 2, 3 ];
console.log( arr ); // [ 1,2,3 ];
const toStr = arr.toString()
console.log( toStr, typeof toStr ); // 1,2,3 string
Alternatively, you can use the syntax shown below:
String(array)
See the example below:
const arr = [ 1, 2, 3 ];
console.log( arr ); // [ 1,2,3 ];
const toStr = String(arr)
console.log( toStr, typeof toStr ); // 1,2,3 string
Alternatively, an array in square notation can be used as a string conversion.
console.log( [] + 1 ); // "1"
console.log( [1] + 1 ); // "11"
console.log( [1,2] + 1 ); // "1,21"
The above conversion is also true for a string.
See the example below:
console.log( "" + 1 ); // "1"
console.log( "1" + 1 ); // "11"
console.log( "1,2" + 1 ); // "1,21"
Identical Arrays Comparison
Two created identical arrays in JavaScript are never the same.
See the example below:
console.log( [] === [] ); // false
console.log( [0] === [0] ); // false
Deleting items/elements in an array
The delete an element in an array, use the syntax shown below:
// delete an item in an array
delete array[item]
// delete multiple items in an array
delete array[item1], delete array[item2], ..., delete array[item3]
A deleted element in an array is reassigned to a new data of undefined
.
See the example below:
const fruits = [ "Apple", "Orange", "Melon", "Banana" ];
console.log( fruits[1] ); // Orange
delete fruits[1];
console.log( fruits[1] ); // undefined
console.log(fruits.length); // 4
The example above is the same as below:
const fruits = [ "Apple", "Orange", "Melon", "Banana" ];
console.log( fruits[1] ); // Orange
fruits[1] = undefined;
console.log(fruits.length); // 4
Happy coding!!!