Home > Article > Web Front-end > There are several ways to write string splicing in es6
There are two ways to write string splicing in es6: 1. Use "${var}" to splice characters in the template string, and the writing method is "`${String 1} String 2`", JS expressions can be placed in "${}"; 2. Use the " " operator and write "String 1 String 2". When using ternary expressions, they need to be enclosed in parentheses.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
ES6 String splicing
String splicing can be done in the following ways:
1. In the template characters Use ${var}
in the string to splice the characters
const name = '小明'; const score = 59; let result = ''; if(score > 60){ result = `${name}的考试成绩及格`; }else{ result = `${name}的考试成绩不及格`; } console.log(result);
Description:
ES6 Added `` (backticks) to declare a string. A string wrapped in backticks is a template string. Template strings can display template strings on multiple lines and also run variable interpolation. You can use any valid JavaScript expression in interpolation. Formulas such as `${2 * 3}` or `${foo()}` ...
In es6, JS expressions can be placed in ${}, and you can operations and reference object properties.
Method 2
Operator splicing characters
Note that when using ternary expressions, they need to be enclosed in parentheses
const name = '小明'; const score = 61; let result = ''; if(score > 60){ result = name+'的考试成绩及格'; }else{ result = name+'的考试成绩及格'; } console.log(result);
[Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of There are several ways to write string splicing in es6. For more information, please follow other related articles on the PHP Chinese website!