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 width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.