totn JavaScript

JavaScript: String normalize() method

This JavaScript tutorial explains how to use the string method called normalize() with syntax and examples.

Description

In JavaScript, normalize() is a string method that is used to convert a string to its Unicode Normalization Form. Because the normalize() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the normalize() method is:

string.normalize([form_type]);

Parameters or Arguments

form_type

Optional. It is the Unicode Normalization Form that you want the method to return. If no parameter is provided, the method uses NFC as the default. It can be one of the following:

Value Description
NFC Normalization Form Canonical Composition (default)
NFD Normalization Form Canonical Decomposition
NFKC Normalization Form Compatibility Composition
NFKD Normalization Form Compatibility Decomposition

Returns

The normalize() method returns a string that contains the Unicode Normalization Form as specified by the form_type parameter.

Note

  • The normalize() method does not change the value of the original string.

Example

Let's take a look at an example of how to use the normalize() method in JavaScript.

For example:

var totn_string1 = 'caf\u00E9';
var totn_string2 = 'cafe\u0301';

console.log(totn_string1);
console.log(totn_string2);
console.log(totn_string1 === totn_string2);
console.log(totn_string1.normalize() === totn_string2.normalize());

In this example, we have declared two variables called totn_string1 and totn_string2 that contain accent values that appear the same but are different Unicode values. We have then tested for equality on the original string values as well as their normalized values.

We have written the output to the web browser console log, for demonstration purposes, to show what the normalize() method returns.

The following will be output to the web browser console log:

café
café
false
true

As you can see, the two string variables totn_string1 and totn_string2 display the same text, even though their accent values are different Unicode values. When we test for equality on these string values using the === operator, false is returned because they are not equal.

After invoking the normalize() method on both totn_string1 and totn_string2 and testing for equality on the normalized values, true is returned. In normalized form (Normalization Form Canonical Composition), these values are equal.