How to insert Unicode in JavaScript

The Unicode Standard provides a unique number for every character, no matter the platform, device, application, or language. UTF-8 is a popular Unicode encoding which has 88-bit code units.

Unicode in Javascript

There are several ways that Unicode may be needed in Javascript. These include:

  • Unicode in Javascript source code.
  • Unicode in Javascript strings.

Code

Unicode in Javascript source code

In Javascript, the identifiers and string literals can be expressed in Unicode via a Unicode escape sequence. The general syntax is \uXXXX, where X denotes four hexadecimal digits. For example, the letter o is denoted as ‘\u006F’ in Unicode. Hence, ​to write the letter “foo” in Unicode, we can use the following code.

var f\u006F\u006F = 'abc';
console.log(foo)

Unicode in Javascript strings

Just like in source code, Unicode can also be represented in a Javascript string ​using the \uXXXX syntax. The following code assigns a special Unicode character (a cow) to the variable abc.

var str = '\uD83D\uDC04';
console.log(str)
Copyright ©2024 Educative, Inc. All rights reserved