JAVASCRIPT Tutorial

Introduction to Arrays

Arrays are fundamental data structures in programming that store an ordered collection of data elements of the same type.

Key Concepts:

  • Square Brackets []: Used to access elements in an array.
  • Index: Represents the position of an element in an array, starting from 0.
  • Length: The number of elements in an array.

Practical Steps for Using Arrays:

  1. Declare an Array: Declare an array using square brackets and a data type, such as int[] arr;.
  2. Initialize an Array: Assign values to the array elements using the assignment operator, such as arr[0] = 1.
  3. Access Elements: Use square brackets with the index to access specific elements, such as int value = arr[1];.
  4. Get Array Length: Use the length property to determine the number of elements in an array.

Javascript Example:

// Declare an array of strings
const names = ["John", "Jane", "Alex"];

// Access the first element
console.log(names[0]); // Outputs "John"

// Get the length of the array
console.log(names.length); // Outputs 3

Simplified Guide for English Speakers:

An array is like a box with compartments that can hold different items. Each item in an array has a number called an index. We can put items into the array and take them out using the index.

  • Create an array: To create an array, we use square brackets and curly braces, like [{"John", "Jane"}.
  • Put items in an array: We can put items into an array by using the index, like array[0] = "John".
  • Take items out of an array: We can take items out of an array by using the index, like let name = array[0].
  • Find out how many items are in an array: We can find out how many items are in an array by using the length property, like let length = array.length.