Home  >  Article  >  Web Front-end  >  There are several ways to write string splicing in es6

There are several ways to write string splicing in es6

青灯夜游
青灯夜游Original
2022-04-11 18:22:116409browse

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.

There are several ways to write string splicing in es6

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);

There are several ways to write string splicing in es6

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);

There are several ways to write string splicing in es6

[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!

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