Tips for Javascript String concatenation (recommended): javascript string
In Javascript, you will often encounter character strings, but it is troublesome to concatenate a character string that is too long.
If it is in a row, the readability is poor. If it is to wrap, an error is reported directly.
This section describes several Javascript String concatenation techniques.
String addition (+)
var items = '<li class="details">' + '<span>Hello world</span>' + '</li>';
Concatenate a string using a backslash
var items = '<li class="details">' \ '<span>Hello world</span>' \ '</li>';
Concatenate strings using Arrays
Convert the array into a string using the join method of the array.
var empList = ['<li class="details">', '<span>Hello world</span>','</li>'].join("");
You can encapsulate a StringBuffer Method Based on the array to complete String concatenation.
function StringBuffer(){ this.buffer = []; } StringBuffer.prototype = { constructor: StringBuffer, append: function(str){ this.buffer.push(str); return this; }, toString: function(){ return this.buffer.join(''); } };
ES6 template string
ES6 introduces a new literal syntax called template string.
Replace the original single quotation marks or double quotation marks with an anti-apostrophes.
Worker ('.warning'worker .html ('
Line breaks, indentation, and spaces in the string are all output to the new string.
To learn about the performance of String concatenation, we recommend that you read the book "high-performance Javascript" by Nicholas C. Zakas.
The above Javascript String concatenation tips (recommended) are all the content shared by the editor. I hope you can give us a reference and support the help house.