Unicode字符一般是21個bit的,而普通的JavaScript字符(大部分)是16bit的,能夠編碼成UTF-16。超過16bit的字符須要用2個常規字符表示。好比,好比下面的的代碼將會輸出一個Unicode小火箭字符(‘\uD83D\uDE80’),你能夠在瀏覽器的console裏試一下:javascript
console.log('\uD83D\uDE80');
在 ECMAScript 6 裏,能夠使用新的表示方法,更簡潔:java
console.log('\u{1F680}');
模板字符串提供了三個有用的語法功能。數組
首先,模板字符串支持嵌入字符串變量:瀏覽器
let first = 'Jane'; let last = 'Doe'; console.log(`Hello ${first} ${last}!`); // Hello Jane Doe!
第二,模板字符串支持直接定義多行字符串:編碼
let multiLine = ` This is a string with multiple lines`;
第三,若是你把字符串加上String.raw前綴,字符串將會保持原始情況。反斜線(\)將不表示轉義,其它專業字符,好比 \n 也不會被轉義:spa
let raw = String.raw`Not a newline: \n`; console.log(raw === 'Not a newline: \\n'); // true
字符串可遍歷循環,你能夠使用 for-of 循環字符串裏的每一個字符:code
for (let ch of 'abc') { console.log(ch); } // Output: // a // b // c
並且,你能夠使用拆分符 (...) 將字符串拆分紅字符數組: ip
let chars = [...'abc']; // ['a', 'b', 'c']
有三個新的方法能檢查一個字符串是否包含另一個字符串:字符串
> 'hello'.startsWith('hell') true > 'hello'.endsWith('ello') true > 'hello'.includes('ell') true
這些方法有一個可選的第二個參數,指出搜索的起始位置:string
> 'hello'.startsWith('ello', 1) true > 'hello'.endsWith('hell', 4) true > 'hello'.includes('ell', 1) true > 'hello'.includes('ell', 2) false
repeat()方法能重複複製字符串:
> 'doo '.repeat(3) 'doo doo doo '