js string splicing encyclopedia

In JavaScript, there are several ways to use string concatenation: connector (+), backtick (`), join(), concat().

Table of contents

1. Use the connector + to string together the strings you want to connect.

2. Template string, backtick (`)

3.Join() splicing in arrays

4. Use the concat() method to concatenate strings


1. Use the connector + to string together the strings you want to connect.

let a = 'java'
let b = a + 'script'
 
//运行结果:javascript

2. Template string, backtick (`)

The new string method in ES6 can be used with anti-single quotes to complete the function of splicing strings

How to type back quotes : Adjust the input method to English input method and click the key to the left of the number key 1 on the keyboard. 

Features: You can wrap string variables with ${} and then write them where they need to be spliced.

let a = 'java'
let b = `hello ${a}script`
 
//运行结果:hello javascript

3.Join() splicing in arrays

let arr = ['hello','java','script']
let str = arr.join("--")
console.log(str) // hello--java--script

4. Use the concat() method to concatenate strings

The concat() method is used to concatenate two or more arrays or strings. This method does not modify the existing array, but only returns the concatenated new array.

let a = 'java'
let b = 'script'
 
let str = a.concat(b)
 
//运行结果:javascript


 

Guess you like

Origin blog.csdn.net/white0718/article/details/131892027