Home  >  Article  >  Web Front-end  >  Basic methods of string concatenation in JavaScript

Basic methods of string concatenation in JavaScript

PHPz
PHPzOriginal
2016-05-16 15:51:121672browse

This article mainly introduces the basic method of string splicing in JavaScript. It is the basic knowledge for introductory learning of JS. Friends who need it can refer to it

It is very simple, just use a " " to join two String "add":

var longString = "One piece " + "plus one more piece.";

To accumulate multiple strings into one string, you can also use the "=" operator:

var result = "";
result += "My name is Anders"
result += " and my age is 25";

To add a string To add a newline character, you need to use the escape character "":

var confirmString = "You did not enter a response to the last " +
"question.Submit form anyway?";
var confirmValue = confirm(confirmString);

But this method can only be used in situations like warnings and confirmation dialog boxes. If this text is used as HTML content Presented, it will be invalid. At this time, replace it with "0c6dc11e160d3b678d68754cc175188a":

var htmlString = "First line of string.<br>Second line of string.";
document.write(htmlString);

String object also provides the method concat(), which completes the same function as " ":

string.concat(value1, value2, ...)

However, the concat() method is obviously not as intuitive and concise as " ".

For more related tutorials, please visit JavaScript Tutorial

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn