How to Create an Array in JavaScript

Arrays are one of the basic JavaScript datatypes, along with numbers, strings, objects, and functions. JavaScript arrays hold a collection of values, each associated with a numerical index or "subscript". Each value can be referred to with the syntax array_name [index]. JavaScript provides a variety of ways to create arrays.

Things You'll Need

  • JavaScript editor
Show More

Instructions

  1. Create an Array in JavaScript

    • 1

      Create an array by declaring a variable with the following syntax. The first value will be assigned an index of 0.
      var array_name = [value, value...]

    • 2

      Create an array with a predetermined number of elements with the following syntax:
      var array_name = new Array ();

    • 3

      Create an array with values initialized by using the new syntax, but providing values instead of element count:
      var array_name = new Array ("Bob", "Alex");

    • 4

      Create an array from parts of a string by calling the split function to break the string apart:
      var string_parts = "Bob,Alex".split (",");

Related Searches:

Comments

You May Also Like

Related Ads

Featured