Tips for Javascript String concatenation (recommended): javascript string

Source: Internet
Author: User
Tags javascript string concatenation

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.