Skip to main content

Array



General

Info:

  • arrays are written with square brackets.
  • Array items are separated by commas.
  • Arrays are special type of objects
    • Elements can be arrays, functions and objects
  • Arrays use numbers to access its "elements".
    • Array indexes start with 0
    • The length property is always one more than the highest array index.

Caution:

  • Comparing string properties are more complex
  • The reduce() method does not reduce the original array
  • The typeof operator in JavaScript returns "object" for arrays.

Danger:

  • Adding elements with high indexes can create undefined "holes" in an array.
  • Using delete leaves undefined holes in the array.
  • Arrays use numbered indexes.
    • If you use named indexes, JavaScript will redefine the array to an object. After that, some array methods and properties will produce incorrect results.
      const person = [];
      person["firstName"] = "John";
      person["lastName"] = "Doe";
      person["age"] = 46;
      person.length; // Will return 0
      person[0]; // Will return undefined
  • JavaScript has a built-in array constructor new Array().

    • Avoid using Array constructor

    • The new keyword can produce some unexpected results:

      // Create an array with one element ???
      const points = new Array(40);
      ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  • sort() method will produce incorrect result when sorting numbers:

    "25" is bigger than "100"


Declaration


const

Caution:

  • Arrays are Not Constants. It defines a constant reference to an array.

Danger:

  • JavaScript const variables must be assigned a value when they are declared:
    const cars;
    cars = ["Saab", "Volvo", "BMW"];
  • An array declared with const cannot be reassigned
    const cars = ["Saab", "Volvo", "BMW"];
    cars = ["Toyota", "Volvo", "Audi"];

var

  • Redeclaring an array declared with var is allowed anywhere

    var cars = ["Volvo", "BMW"];   // Allowed
    var cars = ["Toyota", "BMW"]; // Allowed
    cars = ["Volvo", "Saab"]; // Allowed
  • Arrays declared with var can be initialized at any time.

    • You can even use the array before it is declared:

      cars = ["Saab", "Volvo", "BMW"];
      var cars;

Scope


const

Info:

  • const has Block Scope.

Danger:

  • An array declared in a block is not the same as an array declared outside the block:

    const cars = ["Saab", "Volvo", "BMW"];
    // Here cars[0] is "Saab"
    {
    const cars = ["Toyota", "Volvo", "BMW"];
    // Here cars[0] is "Toyota"
    }
    // Here cars[0] is "Saab"
  • Redeclaring or reassigning an array to const, in the same scope, or in the same block, is not allowed:

    const cars = ["Volvo", "BMW"];
    const cars = ["Volvo", "BMW"];
    var cars = ["Volvo", "BMW"];
    cars = ["Volvo", "BMW"];

    {
    const cars = ["Volvo", "BMW"];
    const cars = ["Volvo", "BMW"];
    var cars = ["Volvo", "BMW"];
    cars = ["Volvo", "BMW"];
    }

var

Info:

  • var has global Scope

    var cars = ["Saab", "Volvo", "BMW"];
    // Here cars[0] is "Saab"
    {
    var cars = ["Toyota", "Volvo", "BMW"];
    // Here cars[0] is "Toyota"
    }
    // Here cars[0] is "Toyota"