Home > Web Front-end > JS Tutorial > body text

Basic methods of string concatenation in JavaScript

PHPz
Release: 2018-10-12 17:19:21
Original
1655 people have browsed it

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.";
Copy after login

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";
Copy after login

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);
Copy after login

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 "
":

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

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

string.concat(value1, value2, ...)
Copy after login

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

For more related tutorials, please visit JavaScript Tutorial

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!