Javascript - Character

About

This article is about the character representation and manipulation in Javascript (ie code point).

They:

You don't need Javascript to show unicode character on HTML. See: HTML - Unicode Character

Creation

from Literal

From a literal

  • A character is just a string with one character
let char = 'a';
console.log(`The character a: ${char}`);
console.log('\u270B');
console.log('\uD83D\uDE00');

from String

let foo = "foo \u270B";
let character = foo.charAt(foo.length-1);
console.log(character);

from Code Point (number)

From a code point (ie the index of the character in the character set).

Example with the High Five character

let hexa = '270B';
let codePoint = parseInt(hexa, 16);
let character = String.fromCodePoint(codePoint);
console.log(`The character with the code point (${codePoint}) is ${character}`);

Length

For one character, you may get a length of one or two in Javascript.

Why ? Because :

Example with the grining face (1F600)

console.log('๐Ÿ˜€'.length);

Other example of character encoded with two code point


Powered by ComboStrap