Home  >  Article  >  Web Front-end  >  What are the backticks used for in es6?

What are the backticks used for in es6?

WBOY
WBOYOriginal
2022-03-30 18:05:592142browse

The functions of backticks in es6: 1. Used to parse variables, the syntax is "`${variable name}`"; 2. Used to implement line breaks, the syntax is "`multi-line code`"; 3. Used to call methods, the syntax is "`${method name()}`".

What are the backticks used for in es6?

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

What are the backticks in es6 used for?

1. Variables can be parsed

var name = `张三`
var sayHello = `Hello,我的名字叫${name}`
console.log(sayHello)

Output result:

What are the backticks used for in es6?

2. You can wrap the line

let obj = {
             name: 'zs',
             age: 18
          }
let html = `
             <div>
                 <span>${obj.name}</span>
                 <span>${obj.age}</span>
             </div>
            `
console.log(html)

The output result is:

What are the backticks used for in es6?

3. You can call the function

const fn=()=>{ return &#39;我是fn函数&#39; }
let html = `我是模板字符串${fn()}`
console.log(html)

The output result is:

What are the backticks used for in es6?

[Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of What are the backticks used for 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