Skip to content
the poor coder

Create array literal

Poor Man
Published
Reading time
1 min read
On this page

Create Array Literal#

An array is a collection of values stored in a single variable. In JavaScript, arrays are created using array literals, which are a list of comma-separated values enclosed in square brackets [].

Example:#

Text
var fruits = ['apple', 'banana', 'orange', 'kiwi'];

In this example, we have created an array of fruits. The values 'apple', 'banana', 'orange', and 'kiwi' are enclosed in square brackets [] and separated by commas. The array is assigned to the variable 'fruits' using the assignment operator (=).

There are multiple ways to create an array literal in JavaScript:

  • You can create an empty array by providing no values inside the square brackets.
  • You can create an array with a single value by providing only one value inside the square brackets.
  • You can create a multidimensional array by nesting arrays inside other arrays.

Empty Array:#

Text
var emptyArray = [];

In this example, we have created an empty array. Since there are no values inside the square brackets [], the array is empty.

Array with a Single Value:#

Text
var singleValueArray = ['hello'];

In this example, we have created an array with a single value. The value 'hello' is enclosed in square brackets [] and assigned to the variable 'singleValueArray'.

Multidimensional Array:#

Text
var multidimensionalArray = [['John', 'Doe'], ['Jane', 'Doe']];

In this example, we have created a multidimensional array. The array contains two nested arrays, each containing two values. The array is assigned to the variable 'multidimensionalArray' using the assignment operator (=).

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC