JavaScript Tutorials

JavaScript String normalize()
Syntax & Examples

String.normalize() method

The normalize() method of the String class in JavaScript returns the Unicode Normalization Form of the calling string value.


Syntax of String.normalize()

There are 2 variations for the syntax of String.normalize() method. They are:

1.
normalize()

This method returns the Unicode Normalization Form of the calling string value using the default normalization form (NFC).

Returns value of type String.

2.
normalize(form)

Parameters

ParameterOptional/RequiredDescription
formoptionalA string representing the Unicode Normalization Form. Possible values are 'NFC', 'NFD', 'NFKC', and 'NFKD'.

This method returns the Unicode Normalization Form of the calling string value using the specified normalization form.

Returns value of type String.



✐ Examples

1 Using normalize() method with default form

In JavaScript, we can use the normalize() method to normalize a string to the default Unicode Normalization Form (NFC).

For example,

  1. We define a string variable str with a value that contains a character in a composed form and its decomposed equivalent.
  2. We use the normalize() method with no arguments to normalize the string to the default form.
  3. The result is stored in the variable normalizedStr.
  4. We log normalizedStr to the console using the console.log() method.

JavaScript Program

const str = '\u1E9B\u0323';
const normalizedStr = str.normalize();
console.log(normalizedStr);

Output


2 Using normalize() method with NFD form

In JavaScript, we can use the normalize() method to normalize a string to the NFD (Normalization Form D) form.

For example,

  1. We define a string variable str with a value that contains a character in composed form.
  2. We use the normalize() method with the argument 'NFD' to normalize the string to the NFD form.
  3. The result is stored in the variable normalizedStr.
  4. We log normalizedStr to the console using the console.log() method.

JavaScript Program

const str = '\u1E9B\u0323';
const normalizedStr = str.normalize('NFD');
console.log(normalizedStr);

Output

ṩ

3 Using normalize() method with NFKC form

In JavaScript, we can use the normalize() method to normalize a string to the NFKC (Normalization Form KC) form.

For example,

  1. We define a string variable str with a value that contains a character in composed form.
  2. We use the normalize() method with the argument 'NFKC' to normalize the string to the NFKC form.
  3. The result is stored in the variable normalizedStr.
  4. We log normalizedStr to the console using the console.log() method.

JavaScript Program

const str = '\uFB01';
const normalizedStr = str.normalize('NFKC');
console.log(normalizedStr);

Output

fi

Summary

In this JavaScript tutorial, we learned about normalize() method of String: the syntax and few working examples with output and detailed explanation for each example.