JavaScript Arrays

What is an Array?

An array is a special variable that can hold multiple values at once. Arrays are useful for storing lists of data, such as numbers, strings, or objects.

Creating Arrays

You can create arrays using the array literal syntax or the Array constructor.

Tutorials dojo strip

Array Literal Syntax

JavaScript

Array Constructor

JavaScript

Accessing Array Elements

You can access array elements using their index. Array indices start at 0.

JavaScript

Modifying Array Elements

You can modify elements in an array by accessing them using their index.

JavaScript

Array Properties

Length

The length property returns the number of elements in an array.

JavaScript

Array Methods

JavaScript provides various methods to manipulate arrays.

1. push()

The push() method adds one or more elements to the end of an array.

JavaScript

2. pop()

The pop() method removes the last element from an array.

JavaScript

3. shift()

The shift() method removes the first element from an array.

JavaScript

4. unshift()

The unshift() method adds one or more elements to the beginning of an array.

JavaScript

5. splice()

The splice() method adds or removes elements from an array.

JavaScript

6. slice()

The slice() method returns a new array containing a portion of an existing array.

JavaScript

7. concat()

The concat() method merges two or more arrays.

JavaScript

8. join()

The join() method joins all elements of an array into a string.

JavaScript

JavaScript Arrays Example Code

Explanation of Code:

  • Creating Arrays: Demonstrates creating arrays using the array literal syntax.
  • Accessing Elements: Accesses elements using their index.
  • Modifying Elements: Modifies elements using their index.
  • Array Properties: Uses the length property to get the number of elements.
  • Array Methods: Demonstrates various array methods such as push, pop, shift, unshift, splice, slice, concat, and join.
HTML

JavaScript Labs

Tutorials dojo strip