Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu October 03, 2022 . #Javascript . #Q&A

How do you write an array in JavaScript?

Creating an array in JavaScript is as easy as cutting a cake.

Array Syntax:

const  array_name = [item1, item2,item3,...];

Use an array to store more than one value.

  1. array_name : we can give the array any name according to our choice.
  2. Inside the [ ] square bracket, we can store more than one value.
    [
     "Red", 
     "Blue", 
     "Green", 
     "Yellow"
    ]
  3. Break and new line are not important.
  4. To store a string in an array, we must use the double or single quote.
  5. We don't need to use any quote to store a number.
    [
     7,
     26,
     478,
     5
    ]

Access an array element.

We can access an array element by referring to the index number.

const colors = ["Red", "Blue", "Green", "Yellow", "Pink"]

Using the colors[0] , we can access the first element of the above array.

save
listen
AI Answer
Write Your Answer
loading
back
View All