Introduction to Arrays
What is an Array in JavaScript?
An array is a data structure that can hold more than one value at a time. It is a collection of elements, each identified by a numeric index.
Examples of Arrays
-
String Array:
const stringArray = ["apple", "banana", "orange"];
-
Number Array:
const numberArray = [1, 2, 3, 4, 5];
-
Boolean Array:
const booleanArray = [true, false, true];
-
Mixed Array:
const mixedArray = ["apple", 2, true, null, undefined];
-
Undefined and Null Array:
const nullUndefinedArray = [undefined, null];
Creating a Custom Array
Here is a basic template for creating a custom array in JavaScript.
class MyArray {
constructor() {
this.length = 0;
this.data = {};
}
}
const myArray = new MyArray();
console.log(myArray);
Output
MyArray { length: 0, data: {} }