--- id: javascript-arrays title: "JavaScript Arrays" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["array literal", "new Array", "array index", "array length", "isArray"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "arrays", "data-structures", "objects"] raw_sources: ["https://www.w3schools.com/js/js_arrays.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Arrays]] ## π― ν μ€ ν΅μ°° (One-line insight) An array is a special object that holds an ordered, zero-indexed collection of values β use `[]` literals to create them, and remember `typeof` reports them as `"object"`. [S1] ## π§ ν΅μ¬ κ°λ (Core concepts) - **Arrays store ordered collections** β a single variable can hold multiple values, accessed by zero-based index numbers. [S1] - **Literal syntax is preferred** β `[]` is the recommended way to create an array (over `new Array()`). [S1] - **Indexes start at 0** β the first element is at index `0`, the second at `1`, and so on. [S1] - **Arrays are objects** β `typeof` returns `"object"`; elements can themselves be objects, functions, or other arrays. [S1] - **No named indexes** β JavaScript does not support arrays with named indexes; using named indexes turns the array into a plain object and breaks array methods. [S1] - **Detect with `Array.isArray()` / `instanceof`** β because `typeof` returns `"object"`, use these to recognize an array. [S1] ## π§© μΆμΆλ ν¨ν΄ (Extracted patterns) - **Create with a literal** β `const cars = ["Saab", "Volvo", "BMW"];`. [S1] - **Access / change by index** β `cars[0]` reads, `cars[0] = "Opel"` writes. [S1] - **Append with `push` or `length`** β `fruits.push("Lemon")` or `fruits[fruits.length] = "Lemon"`. [S1] - **Iterate with `for` or `forEach`** β loop by index, or pass a callback to `forEach`. [S1] ## π μΈλΆ λ΄μ© (Details) **Creating an array (literal)** Using an array literal is the easiest way to create a JavaScript array: [S1] ```javascript const cars = ["Saab", "Volvo", "BMW"]; ``` Spaces and line breaks are not important; a declaration can span multiple lines: [S1] ```javascript const cars = [ "Saab", "Volvo", "BMW" ]; ``` You can also create an array and then provide the elements: [S1] ```javascript const cars = []; cars[0]= "Saab"; cars[1]= "Volvo"; cars[2]= "BMW"; ``` **Using the `new Array()` keyword** The following example also creates an array and assigns values to it, but the array literal method is recommended: [S1] ```javascript const cars = new Array("Saab", "Volvo", "BMW"); ``` **Accessing array elements** You access an array element by referring to its index number. Array indexes start with 0: [S1] ```javascript const cars = ["Saab", "Volvo", "BMW"]; let car = cars[0]; ``` **Changing an array element** This statement changes the value of the first element in `cars`: [S1] ```javascript const cars = ["Saab", "Volvo", "BMW"]; cars[0] = "Opel"; ``` **Accessing the full array** With JavaScript, the full array can be accessed by referring to the array name: [S1] ```javascript const cars = ["Saab", "Volvo", "BMW"]; document.getElementById("demo").innerHTML = cars; ``` **Arrays are Objects** Arrays are a special type of object. The `typeof` operator returns `"object"` for arrays. But JavaScript arrays are best described as arrays. Arrays use *numbers* to access their "elements". This array uses numbers to access its elements: [S1] ```javascript const person = ["John", "Doe", 46]; ``` Objects use *names* to access their "members". This object uses names to access its members: [S1] ```javascript const person = {firstName:"John", lastName:"Doe", age:46}; ``` **Array elements can be objects** JavaScript variables can be objects, and arrays are special kinds of objects. Because of this, you can have variables of different types in the same array, and array elements can themselves be objects or functions: [S1] ```javascript myArray[0] = Date.now; myArray[1] = myFunction; myArray[2] = myCars; ``` **The `length` property** The `length` property of an array returns the number of array elements: [S1] ```javascript const fruits = ["Banana", "Orange", "Apple", "Mango"]; let length = fruits.length; ``` **Looping array elements (for loop)** One way to loop through an array is using a `for` loop: [S1] ```javascript const fruits = ["Banana", "Orange", "Apple", "Mango"]; let fLen = fruits.length; let text = "