Combining Unicode ES6 Literals with ES6 Template Literals

If I want to print the Chinese Unicode character in javascript ES6 / ES2015, I can do this:

console.log(`\u{4eb0}`);

Similarly, if I want to interpolate a variable into a string literal of a pattern, I can do this:

let x = "48b0";
console.log(`The character code is ${ x.toUpperCase() }.`);

However, it seems that I cannot combine the two to print a list of, for example, 40 consecutive Unicode Chinese characters. This does not work:

for (let i = 0, firstCharCode = parseInt("4eb0", 16); i < 40; ++i) {
    let hexCharCode = (firstCharCode + i).toString(16);
    console.log(`\u{${ hexCharCode }}`); // generates SyntaxError
}

So, I ask if there is a way that this is possible.

+4
source share
1 answer

You will need to use String.fromCodePoint()one that takes a number and returns a character.

, ... ... . , .

+5

Source: https://habr.com/ru/post/1658325/


All Articles