Interesting Facts about JavaScript Arrays
Last Updated :
10 Nov, 2024
Let us talk about some interesting facts about JavaScript Arrays that can make you an efficient programmer.
Arrays are Objects
JavaScript arrays are actually specialized objects, with indexed keys and special properties. They have a length property and are technically instances of the Array constructor.
JavaScript
const a = [10, 20, 30];
console.log(typeof a);
You can add non-integer properties to arrays, making them work partly like objects. However, this is usually discouraged for clarity.
JavaScript
let a = [1, 2, 3];
// Adding a property to array (NOT RECOMMEMDED IN PRACTICE)
a.name = "MyArray";
console.log(a.name);
// Iterating through the array with `for...in`
// (NOT RECOMMENDED IN PRACTICE)
for (let key in a) {
console.log(`${key}: ${a[key]}`);
}
OutputMyArray
0: 1
1: 2
2: 3
name: MyArray
Mixed Elements Allowed
Like Python and unlike C/C++/Java,, we can have mixed type of elements in a JavaScript array.
JavaScript
const a = [10, "hi", true];
console.log(a);
Dynamic Size
Like Python and unlike C/C++/Java,, the default array implementation is Dynamic Size.
JavaScript
let a = [1, 2, 3];
a.push(4);
console.log(a);
Negative Indexing
The .at() method, introduced in ES2022, allows access to array elements using negative indices, making it easy to retrieve elements from the end of an array without calculating the length.
JavaScript
const arr = [10, 20, 30];
console.log(arr.at(-1));
Resizing an Array
We can resize a JavaScript array by simply changing its length property.
JavaScript
let a = [1, 2, 3, 4];
a.length = 2;
console.log(a);
Array Assignment
When we assign an array to another, it only creates one more reference to the same array.
JavaScript
// changed the original array
let a = [10, 20];
let b = a;
b.push(30);
console.log(a);
Spread Operator
We get members of an array or a string. It helps us in copying in copying an array, concatenating arrays and passing array elements to different parameters of a function.
JavaScript
// Arrat copy using spread operator
let a = [10, 20];
let b = [...a];
b.push(30);
console.log(a);
JavaScript
// Array concatenation using spread operator
let a = [10, 20];
let b = [30, 40];
let c = [...a, ...b]
console.log(c);
JavaScript
function add(x, y, z) {
return x + y + z;
}
let a = [10, 20, 30];
console.log(add(...a));
Empty Elements in Array
JavaScript allows empty elements in an array. When we access these elements, we get undefined.
JavaScript
const a = [1, , , 3];
console.log(a);
console.log(a[1]);
Output[ 1, <2 empty items>, 3 ]
undefined
JavaScript
const a = [1, 3];
a.length = 4
console.log(a);
console.log(a[2]);
Output[ 1, 3, <2 empty items> ]
undefined
Direct Methods to Modify Arrays
JavaScript allows multiple direct methods for efficient programming.
JavaScript
const a = [1, 2, 3, 4];
const b = a.map(x => x * 2);
console.log(b)
const c = a.filter(x => x > 2);
console.log(c)
const d = [1, [2, 3], [4, [5]]];
console.log(d.flat(2));
Output[ 2, 4, 6, 8 ]
[ 3, 4 ]
[ 1, 2, 3, 4, 5 ]
Similar Reads
Interesting Facts about Object in JavaScript Let's see some interesting facts about JavaScript Objects that can help you become an efficient programmer.JavaSctipt Objects internally uses Hashing that makes time complexities of operations like search, insert and delete constant or O(1) on average. It is useful for operations like counting frequ
4 min read
JavaScript - Insert Element in an array In JavaScript elements can be inserted at the beginning, end, and at any specific index. JS provides several methods to perform the operations.At the Beginning This operation inserts an element at the start of the array. The unshift() method is commonly used, which mutates the original array and ret
2 min read
What are Associative Arrays in JavaScript ? Associative arrays in JavaScript, commonly referred to as objects, are crucial for storing key-value pairs. This guide explores the concept and usage of associative arrays, providing insights into their benefits and applications. Example: // Creating an associative array (object)let arr= { name: "Ge
2 min read
JavaScript Array() Constructor The Array() constructor is used to create Array objects and the array constructor can be called with or without a new keyword, both can create a new Array.Syntax:new Array(Value1, Value2, ...);new Array(ArrayLength);Array(Value1, Value2, ...);Array(ArrayLength);Parameters: ValueN: An array initializ
2 min read
JavaScript Array Iteration Methods JavaScript Array iteration methods perform some operation on each element of an array. Array iteration means accessing each element of an array. There are some examples of Array iteration methods are given below: Using Array forEach() MethodUsing Array some() MethodUsing Array map() MethodMethod 1:
3 min read