Template string (★★★)
ES6 create a string of new ways to use the definition of anti-quotes
let name = `zhangsan`;
Templates can parse the string variable
let name = '张三';
let sayHello = `hello,my name is ${name}`; // hello, my name is zhangsan
Template string can wrap
let result = {
name: 'zhangsan',
age: 20,
sex: '男'
}
let html = ` <div>
<span>${result.name}</span>
<span>${result.age}</span>
<span>${result.sex}</span>
</div> `;
In the template string can call the function
const sayHello = function () {
return '哈哈哈哈 追不到我吧 我就是这么强大';
};
let greet = `${sayHello()} 哈哈哈哈`;
console.log(greet); // 哈哈哈哈 追不到我吧 我就是这么强大 哈哈哈哈
Examples of the method: startsWith () and endsWith ()
- startsWith (): whether the parameter string representing the head of the string, returns a Boolean value
- endsWith (): a parameter indicating whether the string at the end of the string, returns a Boolean value
let str = 'Hello world!';
str.startsWith('Hello') // true
str.endsWith('!') // true
Examples of the method: repeat ()
The method represents a repeat n times the original string, returns a new string
'x'.repeat(3) // "xxx"
'hello'.repeat(2) // "hellohello"