Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

What is an array?

Arrays in Javascript

  1. An array is a collection of data items
  2. These items are similar types
  3. An array can also be called a list
Properties an array
  1. An array can contain a group of elements of a similar type
  2. Array elements are stored in contiguous memory blocks
  3. Arrays use numeric indexing
  4. Arrays index starts with0 and ends with size-1
  5. Size of the array can be dynamic and static
  6. Element of an array can be accessed using its index
Syntax of an array
  1. The array can be defined with help of square brackets
  2. It varies from language to language but square brackets are used to define an array
// Define array in c
int prices[4] = {10, 20, 30, 40};

// Define array in Javascript
let prices = [10, 20, 30, 40];

// Define array in python
prices = [10, 20, 30, 40]
Accessing an element
  1. An element can be accessed using an index
  2. Let’s access an element
// Define array in Javascript
let prices = [10, 20, 30, 40];
console.log(prices[2]);
// 30
Updating an index value
  1. An array index value can be updated by overriding a value
  2. Let’s update some values
// Define array in Javascript
let prices = [10, 20, 30, 40];

// Update index o value to 5
prices[0] = 5;
console.log(prices);
// [5, 20, 30, 40]