Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods of concatenating strings in es6

Shulou Source: shulou.com Published: 2022-05-31 23:46:47 09月17日 Update

What are the knowledge points of this article "what are the methods of concatenating strings in es6?" most people do not understand, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what are the methods of concatenating strings in es6?"

Es6 concatenation string there are two ways to write: 1, in the template string using "${var}" to concatenate characters, written as "`${string 1} string 2`", "${}" can be put into the JS expression; 2, the use of the "+" operator, write "string 1 + string 2", the use of ternary expression, the need to use parentheses.

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

ES6 string concatenation

Strings can be concatenated in the following ways:

1. Use ${var} to concatenate characters in the template string

Const name = 'Xiaoming'; const score = 59 leading result =''; if (score > 60) {result = `$ {name} failed in the examination`;} else {result = `$ {name} failed in the examination`;} console.log (result)

Description:

ES6 has added ``(back quotation marks) to declare that the string wrapped in backquotes is a template string template string can be displayed on multiple lines and variable insertion can be run you can use any valid JavaScript expression such as `$ {2 * 3} `or `$ {foo ()}` in interpolation.

In es6, you can put JS expressions in ${}, perform operations, and reference object properties.

Mode 2 + operator concatenation characters

Note that when using ternary expressions, you need to enclose them in parentheses

Const name = 'Xiao Ming'; const score = 61'; if (score > 60) {result = name+' passed';} else {result = name+' passed';} console.log (result)

The above is about the content of this article on "what are the methods of concatenating strings in es6?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Characters strings content templates expressions scores exams methods writing operations quotation marks parentheses articles methods knowledge articles operators Xiaoming ternary valid Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info NVidia MySQL Apple