Languages
[Edit]
EN

JavaScript - code style for array (style guide - correct formatter for array)

3 points
Created by:
jarlh
635

In this article, we would like to show you code style for array in JavaScript.

Quick solution:

const users = [
  'first user',
  'second user'
];

Note:
The main principle of creating an array is to make our code look clean.

Simple array example

To create an array we use square brackets [] by opening - [ and closing - ]; array in separate lines when our items are complex or just long. We place each item in a new line between the opening and closing bracket. As a separator for items we use comas ,.

Example for short/simple items:

const letters = ['a', 'b', 'c'];

Example for long/complex items: 

const users = [
  'first user',
  'second user',
  'third user'
];

Array of objects example

Each object in the array should be placed in a new line and separated with coma ,.

Example:

const users = [
  {name: 'Thomas', age: 21},
  {name: 'Mark', age: 35},
  {name: 'John', age: 29}
];

Array of arrays example

For nested arrays, we mix the above rules - short items in one line, long or complex items in the separated line.

Example:

const array = [
  ['ID', 'Name', 'Age'],
  [
    {id: 1, name: 'Bob', age: 25},
    {id: 2, name: 'Adam', age: 43},
    {id: 3, name: 'Mark', age: 16},
    {id: 4, name: 'John', age: 29}
  ]
];

References:

Alternative titles

  1. JavaScript - default array formatter
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Dirask - content writing

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join