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 - How to show an Unicode Character in HTML

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





Discover More
Javascript - String

The in javascript. A string in JavaScript is encoded with the ucs-2 16-bit character set. An element of a JavaScript string is therefore a 16-bit code unit. code unitscode pointssurrogate pair Strings...
Data System Architecture
Unicode - Surrogate pair (UTF-16)

A surrogate pair is two 16-bit code units used in UTF-16 (16-bit - two-byte) that represents a character above the maximum value stored in 16bit. (ie 0xFFFF hexa or 65535 decimal) Why ? Because the whole...



Share this page:
Follow us:
Task Runner