JS Tutorial



JS ARRAYS


JavaScript Arrays

A JavaScript Array is a special type of object used to store multiple values in a single variable. Arrays are zero-indexed and dynamic (can grow or shrink).

Note: Arrays can contain elements of any type โ€” numbers, strings, objects, other arrays, or mixed types.

๐Ÿ“Œ Array Syntax

let fruits = ["Apple", "Banana", "Mango"];
let numbers = [10, 20, 30, 40];
let mixed = [1, "hello", true];
  

๐Ÿงช Try It Yourself:

Open your browser console and try this code:

let colors = ["Red", "Green", "Blue"];
console.log(colors[0]);       // Red
console.log(colors.length);   // 3
colors.push("Yellow");        // Add new item
console.log(colors);          // ["Red", "Green", "Blue", "Yellow"]
colors.pop();                 // Remove last item
  

๐Ÿงฐ Common Array Methods

  • push() โ€“ Adds item to end
  • pop() โ€“ Removes item from end
  • shift() โ€“ Removes item from start
  • unshift() โ€“ Adds item to start
  • slice(start, end) โ€“ Extracts a portion
  • splice(start, deleteCount) โ€“ Removes or replaces elements
  • indexOf() โ€“ Finds index of item
  • includes() โ€“ Checks if item exists
  • join() โ€“ Converts to string

๐Ÿ”ง Live Example: Dynamic Array Display


๐Ÿง  Array Iteration

let numbers = [10, 20, 30];

numbers.forEach(function(num) {
  console.log(num);
});

// Using arrow function
numbers.forEach(num => console.log(num));
  

๐Ÿ’ก Tips

  • Arrays are objects in JavaScript โ€” typeof [] === "object"
  • Use Array.isArray() to check if a variable is an array.
  • Use modern methods like map(), filter(), and reduce() for powerful array operations.

Try This:

Type ["one", "two", "three"].join(" | ") in the console and observe how it combines the array into a single string.


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review