The number of predefined properties that an array supports are huge and It’s not always possible to remember each and every property.
So, here I have mentioned few selective properties and usage of an array that comes in handy at times!.
let arr = [14, 'hello', null];
let arr = new Array(14, 'hello', null);
[elem1, elem2, ...elemN]
var myArray = new Array(); var myArray = ;
var myArray = ; myArray = [10, 20, 30]; // [30, 20, 30] myArray = 28; // [28, 20, 30] myArray.push(78); // [28, 20, 30, 78]
Using ‘new’ keyword to create an array
There is a very slight difference while using the ‘new’ keyword to create an array.
let arr = new Array(14); console.log(arr); //[empty × 14]
If we pass a single parameter of any type other than an integer, then the array will be initialized with that single element.
let arr = new Array('fourteen'); console.log(arr); // ["fourteen"]
In the case of multiple parameters, the array will be created with all the parameters initialized as it’s elements.
let arr = new Array(13, 14, 15); console.log(arr); //13, 14, 15
Accessing array elements
Accessing array elements is very easy. An array stores all its elements with a numeric index reference. Hence, to access any particular element we have to use the index like below.
let arr = new Array(13, 14, 15); console.log(arr); // 14
let arr = new Array(13, 14, 15); console.log(arr.length); //3
Here are some facts related to array. length that may create confusion for any new developer.
Associative array elements don’t effect array length:
Yes, that’s true! see the below example:
let arr = new Array(13, 14, 15); console.log(arr.length); // 3 arr['element1'] = 40; arr['element2'] = 60; console.log(arr.length); // 3
Confusing? not really. Because associative indexes are assigned as an array’s property. So we can access them using a ‘.’ operator.
Continuing the above example, we can access associative values like below:
Now, why this ‘.’ operator works with an array? Because arrays are one type of object. And by using ‘.’ operator we are doing nothing but accessing an object property. Check below code.
let arr = new Array(13, 14, 15); console.log( typeof(arr.length)); // Object
How to check if a variable is an array or object
Sometimes it is necessary to know what type of variable we are using. So that, dynamically we can utilize inbuilt properties. Properties such as ‘length’, which returns the length of an array but not of an object.
For such purposes, there two methods which we use to determine whether a variable is an array or object.
let arr = [1,2,3]; // method 1 console.log(Array.isArray(arr)); //true // method 2 console.log(arr instanceof Array); //true
There are many ways of removing elements from an array. Each of these methods has different use cases.
Removes and returns an element from the end of an array.
var arr = [10, 20, 30, 40, 50, 60]; arr.pop(); // returns 60 console.log( arr ); // [10, 20, 30, 40, 50]
Removes and returns an element from the beginning of an array.
var arr = [10, 20, 30, 40, 50, 60]; arr.shift(); // returns 10 console.log( arr ); // [20, 30, 40, 50, 60]
Removes an element from any specified position of an array.
var arr = [10, 20, 30, 40, 50]; var removed = arr.splice(3,1); // the first parameter is the index position where to start // ther second parameter is the number of elements to be deleted console.log(arr); // [10, 20, 30, 50]
**Splice can be used to add elements in an array as well.
Deletes the specified index element value of an array and assigns undefined to that index. Hence the length of array never changes.
var arr = [10, 20, 30, 40]; delete arr; // delete element with index 2 console.log( arr ); // [10, 20, undefined, 40]
First, let’s look at the below example:
var arr = [10, 20, , , 50]; console.log(arr); // [10, 20, empty × 2, 50] console.log(arr.length); // 5
In the above code snippet, the array contains 3 elements but it reserves space for 5 elements. This type of array is called a sparse array where the length and number of elements don’t match.