let text = 'abc' for (let i of text) { console.log(i); } //a //b //c
var s = 'Hello world!'; s.startsWith('Hello') // true s.endsWith('!') // true s.includes('o') // true //三个方法都支持第二个参数,表示开始搜索的位置 s.startsWith('world', 6) // true s.endsWith('Hello', 5) // true 向前搜索 s.includes('Hello', 6) // false
'x'.repeat(3) // "xxx" 'hello'.repeat(2) // "hellohello" 'na'.repeat(0) // ""
第一个参数用来指定字符串的最小长度,第二个参数是用来补全的字符串
'x'.padStart(5, 'ab') // 'ababx' 'x'.padStart(4, 'ab') // 'abax' 'x'.padEnd(5, 'ab') // 'xabab' 'x'.padEnd(4, 'ab') // 'xaba' //原字符串的长度,等于或大于指定的最小长度,则返回原字符串 'xxx'.padStart(2, 'ab') // 'xxx' 'xxx'.padEnd(2, 'ab') // 'xxx' //补全的字符串与原字符串,两者的长度之和超过了指定的最小长度,则会截去超出位数的补全字符串 'abc'.padStart(10, '0123456789')// '0123456abc' //省略第二个参数,则会用空格补全长度 'x'.padStart(4) // ' x' 'x'.padEnd(4) // 'x '
传统的JavaScript语言,输出摸板通常是这样的
$("#result").append( "There are <b>" + basket.count + "</b> " + "items in your basket, " + "<em>" + basket.onSale + "</em> are on sale!" );
ES6引入了模板字符串
$("#result").append(` There are <b>${basket.count}</b> items in your basket, <em>${basket.onSale}</em> are on sale! `);
常用写法
// 普通字符串 `In JavaScript '\n' is a line-feed.` // 多行字符串 `In JavaScript this is not legal.` console.log(`string text line 1 string text line 2`); // 字符串中嵌入变量 var name = "Bob", time = "today"; `Hello ${name}, how are you ${time}?`
模板字符串中嵌入变量,需要将变量名写在 之 中 ,{}可以看作javascript的运行环境,因此还可以写方法调用等js代码
var s = 'hello, world' s.substring(0, 5); // 从索引0开始到5(不包括5),返回'hello' s.substring(7); // 从索引7开始到结束,返回'world'