]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-useless-concat.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-useless-concat.md
1 ---
2 title: no-useless-concat
3 layout: doc
4 rule_type: suggestion
5 ---
6
7
8 It's unnecessary to concatenate two strings together, such as:
9
10 ```js
11 var foo = "a" + "b";
12 ```
13
14 This code is likely the result of refactoring where a variable was removed from the concatenation (such as `"a" + b + "b"`). In such a case, the concatenation isn't important and the code can be rewritten as:
15
16 ```js
17 var foo = "ab";
18 ```
19
20 ## Rule Details
21
22 This rule aims to flag the concatenation of 2 literals when they could be combined into a single literal. Literals can be strings or template literals.
23
24 Examples of **incorrect** code for this rule:
25
26 ::: incorrect
27
28 ```js
29 /*eslint no-useless-concat: "error"*/
30 /*eslint-env es6*/
31
32 var a = `some` + `string`;
33
34 // these are the same as "10"
35 var a = '1' + '0';
36 var a = '1' + `0`;
37 var a = `1` + '0';
38 var a = `1` + `0`;
39 ```
40
41 :::
42
43 Examples of **correct** code for this rule:
44
45 ::: correct
46
47 ```js
48 /*eslint no-useless-concat: "error"*/
49
50 // when a non string is included
51 var c = a + b;
52 var c = '1' + a;
53 var a = 1 + '1';
54 var c = 1 - 2;
55 // when the string concatenation is multiline
56 var c = "foo" +
57 "bar";
58 ```
59
60 :::
61
62 ## When Not To Use It
63
64 If you don't want to be notified about unnecessary string concatenation, you can safely disable this rule.