--- 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 = ""; ``` **Looping array elements (forEach)** You can also use the `Array.forEach()` function: [S1] ```javascript const fruits = ["Banana", "Orange", "Apple", "Mango"]; let text = ""; function myFunction(value) { text += "
  • " + value + "
  • "; } ``` **Adding array elements** The easiest way to add a new element to an array is using the `push()` method: [S1] ```javascript const fruits = ["Banana", "Orange", "Apple"]; fruits.push("Lemon"); ``` A new element can also be added to an array using the `length` property: [S1] ```javascript const fruits = ["Banana", "Orange", "Apple"]; fruits[fruits.length] = "Lemon"; ``` **Associative arrays** Many programming languages support arrays with named indexes (associative arrays). JavaScript does *not* support arrays with named indexes. In JavaScript, arrays always use numbered indexes. If you use named indexes, JavaScript will redefine the array as an object, and array methods and properties will produce incorrect results. [S1] **The difference between arrays and objects** In JavaScript, arrays use *numbered* indexes, while objects use *named* indexes. Use an object when you want the element names to be strings (text); use an array when you want the element names to be numbers. [S1] **How to recognize an array** A common question is: how do I know if a variable is an array? The problem is that the `typeof` operator returns `"object"`. The first solution is `Array.isArray()`: [S1] ```javascript Array.isArray(fruits); ``` The second solution is the `instanceof` operator, which returns true if an object is created by a given constructor: [S1] ```javascript const fruits = ["Banana", "Orange", "Apple"]; (fruits instanceof Array); ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's snippets are the applied cases: building a `cars`/`fruits` array, reading/writing by index, rendering a `