Skip to main content

Learning to use js template strings.

 Why does js introduce template strings?

  • This new feature was added in ES6, which means that js also supports templates, which are represented by backticks `.Called a string literal, it's a template string. It gives JS a simple string interpolation feature as well. Why is it simple, because it can't automatically escape special characters, it can't handle dates, times, etc. in special language formats, and it doesn't support looping.Currently it's just something that handles output strings, not a replacement for a template engine.

  • Let's get to know it together.

  • Insert variables in template strings.
Traditional string concatenation uses +, which is very cumbersome, but now, with ES6 syntax, string concatenation can be written like this:
  var a="123";
        var b="hello"+a+"hhh"+a;//before
        var c=`hello${a}hhh${a}`;//now
        console.log(c)
  • Line breaks are allowed in template strings.
Template strings support newlines, which makes the code very nice and clear.
 let str=`<ul>
                <li>${xxx}</li>
                <li>${xxx}</li>
                <li>${xxx}</li>
            </ul>`;
  • Template strings support nested use, as well as the use of expressions.
    let array= [
    {
      id: 1001,
      name: "aaa"
    },
    {
        id:1002,
        name:"bbb"
    }
  ]
 
  const str = `
    <ul>
      ${array.map((item)=>`
      <li>
        ${item.id} ,${item.name?333:0000}}
      </li>
      `)}
    </ul>
  `
  console.log(str);
  • Functions can be called in template strings. The position in the string where the function is called will display the return value after the function is executed.
function fun(){
    return "666";
  }
  console.log(`hello${fun()}-111`);
Summary: A total of four usage scenarios have been introduced above, and more usage scenarios need to be explored by yourself!

Comments

Popular posts from this blog

What is the difference between the box-size property content-box and border-box in the css box model?

 The box model is a very important concept in CSS layout, it includes content area, padding, border, and margin. Box models can be divided into two types: standard box models and IE box models. The box model, as the name suggests, is used to hold things, and the things it holds are the content of HTML elements. In other words, every visible HTML element is a box.

Js uses recursive way to traverse the dom tree to dynamically create element nodes

 What is a dom tree? In short, DOM is the Document Object Model, which provides a structured representation for documents and defines how to access the document structure through scripts. DOM is composed of nodes. After the HTML is loaded, the rendering engine will generate a DOM tree in memory based on the HTML document. This article uses a small case to traverse the dom tree recursively. The core of the method is to determine whether the incoming data is an array, and then traverse the root node. Note that there must be an end condition when using recursion.