JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Arrays


📘 JavaScript Arrays – Storing Multiple Values in One Variable

An array is a special variable that can hold multiple values at once, ordered and accessible by an index.

Unlike variables that hold a single value, arrays are containers for a list of values.


🔹 Creating Arrays

// Using array literals
let fruits = ["Apple", "Banana", "Cherry"];

// Using the Array constructor
let numbers = new Array(10, 20, 30);

// Creating an empty array
let empty = [];

🔹 Array Indexing

  • Arrays are zero-indexed:
    fruits[0] returns "Apple"

  • You can access, modify, or add elements using the index.

console.log(fruits[1]);        // Banana
fruits[2] = "Mango";           // Change "Cherry" to "Mango"

🔹 Array Properties and Methods

Property/Method Description Example
length Returns number of elements fruits.length → 3
push() Adds item at the end fruits.push("Orange")
pop() Removes last item fruits.pop()
shift() Removes first item fruits.shift()
unshift() Adds item at the beginning fruits.unshift("Lemon")
indexOf() Returns index of item or -1 fruits.indexOf("Mango")
includes() Checks if item exists fruits.includes("Banana")
toString() Converts array to comma-separated string fruits.toString()
join() Joins array elements with custom sep fruits.join(" - ")

🔹 Example

let colors = ["Red", "Green", "Blue"];

console.log(colors.length);         // 3
colors.push("Yellow");              // Add "Yellow"
console.log(colors[3]);             // Yellow
colors.pop();                       // Remove "Yellow"
console.log(colors.join(", "));     // Red, Green, Blue

Practice Questions

Q1. How do you create an array of three fruits: Apple, Banana, and Mango?

Q2. What will colors[0] return if colors = ["Red", "Green", "Blue"]?

Q3. How do you add an item "Orange" to the end of an array?

Q4. What method removes the last element from an array?

Q5. How do you check whether an item "Banana" exists in an array?

Q6. What is the output of fruits.length if fruits = ["Apple", "Banana"]?

Q7. How do you remove the first element from an array?

Q8. Which method adds an element to the beginning of an array?

Q9. How do you convert an array into a string separated by dashes (-)?

Q10. How do you find the index of "Mango" in an array?


JS Arrays Quiz

Q1: Which of the following creates an array in JavaScript?

A. let arr = "Apple, Banana";
B. let arr = ["Apple", "Banana"];
C. let arr = (Apple, Banana);
D. let arr = {Apple, Banana};

Q2: What is the index of the first element in an array?

A. 1
B. -1
C. 0
D. Depends on browser

Q3: Which method adds an item at the end of an array?

A. push()
B. pop()
C. shift()
D. join()

Q4: What will ["a", "b", "c"].pop() return?

A. a
B. c
C. b
D. undefined

Q5: Which method removes the first element of an array?

A. shift()
B. pop()
C. unshift()
D. slice()

Q6: Which of these methods checks if an item exists in an array?

A. exists()
B. inArray()
C. includes()
D. find()

Q7: What does join("-") do to an array ["a", "b", "c"]?

A. Returns "abc"
B. Returns "a-b-c"
C. Returns [a-b-c]
D. Joins only first two elements

Q8: Which method adds an element to the start of the array?

A. push()
B. start()
C. shift()
D. unshift()

Q9: What is the result of ["Apple", "Banana"].indexOf("Banana")?

A. 1
B. 2
C. 0
D. -1

Q10: What does the length property return for let arr = [1, 2, 3, 4];?

A. 3
B. 4
C. 5
D. undefined

Go Back Top