EN
JavaScript - code style for array (style guide - correct formatter for array)
3 points
In this article, we would like to show you code style for array in JavaScript.
Quick solution:
xxxxxxxxxx
1
const users = [
2
'first user',
3
'second user'
4
];
Note:
The main principle of creating an array is to make our code look clean.
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:
xxxxxxxxxx
1
const letters = ['a', 'b', 'c'];
Example for long/complex items:
xxxxxxxxxx
1
const users = [
2
'first user',
3
'second user',
4
'third user'
5
];
Each object in the array should be placed in a new line and separated with coma ,
.
Example:
xxxxxxxxxx
1
const users = [
2
{name: 'Thomas', age: 21},
3
{name: 'Mark', age: 35},
4
{name: 'John', age: 29}
5
];
For nested arrays, we mix the above rules - short items in one line, long or complex items in the separated line.
Example:
xxxxxxxxxx
1
const array = [
2
['ID', 'Name', 'Age'],
3
[
4
{id: 1, name: 'Bob', age: 25},
5
{id: 2, name: 'Adam', age: 43},
6
{id: 3, name: 'Mark', age: 16},
7
{id: 4, name: 'John', age: 29}
8
]
9
];