docs/src/rules/array-bracket-spacing.md
A number of style guides require or disallow spaces between array brackets and other tokens. This rule applies to both array literals and destructuring assignments (ECMAScript 6).
var arr = [ 'foo', 'bar' ];
var [ x, y ] = z;
var arr = ['foo', 'bar'];
var [x,y] = z;
This rule enforces consistent spacing inside array brackets.
This rule has a string option:
"never" (default) disallows spaces inside array brackets"always" requires one or more spaces or newlines inside array bracketsThis rule has an object option for exceptions to the "never" option:
"singleValue": true requires one or more spaces or newlines inside brackets of array literals that contain a single element"objectsInArrays": true requires one or more spaces or newlines between brackets of array literals and braces of their object literal elements [ { or } ]"arraysInArrays": true requires one or more spaces or newlines between brackets of array literals and brackets of their array literal elements [ [ or ] ]This rule has an object option for exceptions to the "always" option:
"singleValue": false disallows spaces inside brackets of array literals that contain a single element"objectsInArrays": false disallows spaces between brackets of array literals and braces of their object literal elements [{ or }]"arraysInArrays": false disallows spaces between brackets of array literals and brackets of their array literal elements [[ or ]]This rule has built-in exceptions:
"never" (and also the exceptions to the "always" option) allows newlines inside array brackets, because this is a common pattern"always" does not require spaces or newlines in empty array literals []Examples of incorrect code for this rule with the default "never" option:
:::incorrect
/*eslint array-bracket-spacing: ["error", "never"]*/
var arr = [ 'foo', 'bar' ];
var arr = ['foo', 'bar' ];
var arr = [ ['foo'], 'bar'];
var arr = [[ 'foo' ], 'bar'];
var arr = [ 'foo',
'bar'
];
var [ x, y ] = z;
var [ x,y ] = z;
var [ x, ...y ] = z;
var [ ,,x, ] = z;
:::
Examples of correct code for this rule with the default "never" option:
:::correct
/*eslint array-bracket-spacing: ["error", "never"]*/
var arr = [];
var arr = ['foo', 'bar', 'baz'];
var arr = [['foo'], 'bar', 'baz'];
var arr = [
'foo',
'bar',
'baz'
];
var arr = ['foo',
'bar'
];
var arr = [
'foo',
'bar'];
var [x, y] = z;
var [x,y] = z;
var [x, ...y] = z;
var [,,x,] = z;
:::
Examples of incorrect code for this rule with the "always" option:
:::incorrect
/*eslint array-bracket-spacing: ["error", "always"]*/
var arr = ['foo', 'bar'];
var arr = ['foo', 'bar' ];
var arr = [ ['foo'], 'bar' ];
var arr = ['foo',
'bar'
];
var arr = [
'foo',
'bar'];
var [x, y] = z;
var [x,y] = z;
var [x, ...y] = z;
var [,,x,] = z;
:::
Examples of correct code for this rule with the "always" option:
:::correct
/*eslint array-bracket-spacing: ["error", "always"]*/
var arr = [];
var arr = [ 'foo', 'bar', 'baz' ];
var arr = [ [ 'foo' ], 'bar', 'baz' ];
var arr = [ 'foo',
'bar'
];
var arr = [
'foo',
'bar' ];
var arr = [
'foo',
'bar',
'baz'
];
var [ x, y ] = z;
var [ x,y ] = z;
var [ x, ...y ] = z;
var [ ,,x, ] = z;
:::
Examples of incorrect code for this rule with the "always", { "singleValue": false } options:
:::incorrect
/*eslint array-bracket-spacing: ["error", "always", { "singleValue": false }]*/
var foo = [ 'foo' ];
var foo = [ 'foo'];
var foo = ['foo' ];
var foo = [ 1 ];
var foo = [ 1];
var foo = [1 ];
var foo = [ [ 1, 2 ] ];
var foo = [ { 'foo': 'bar' } ];
:::
Examples of correct code for this rule with the "always", { "singleValue": false } options:
:::correct
/*eslint array-bracket-spacing: ["error", "always", { "singleValue": false }]*/
var foo = ['foo'];
var foo = [1];
var foo = [[ 1, 1 ]];
var foo = [{ 'foo': 'bar' }];
:::
Examples of incorrect code for this rule with the "always", { "objectsInArrays": false } options:
:::incorrect
/*eslint array-bracket-spacing: ["error", "always", { "objectsInArrays": false }]*/
var arr = [ { 'foo': 'bar' } ];
var arr = [ {
'foo': 'bar'
} ]
:::
Examples of correct code for this rule with the "always", { "objectsInArrays": false } options:
:::correct
/*eslint array-bracket-spacing: ["error", "always", { "objectsInArrays": false }]*/
var arr = [{ 'foo': 'bar' }];
var arr = [{
'foo': 'bar'
}];
:::
Examples of incorrect code for this rule with the "always", { "arraysInArrays": false } options:
:::incorrect
/*eslint array-bracket-spacing: ["error", "always", { "arraysInArrays": false }]*/
var arr = [ [ 1, 2 ], 2, 3, 4 ];
var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ];
:::
Examples of correct code for this rule with the "always", { "arraysInArrays": false } options:
:::correct
/*eslint array-bracket-spacing: ["error", "always", { "arraysInArrays": false }]*/
var arr = [[ 1, 2 ], 2, 3, 4 ];
var arr = [[ 1, 2 ], 2, [ 3, 4 ]];
:::
You can turn this rule off if you are not concerned with the consistency of spacing between array brackets.