Using String normalize() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the string approach known as 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() approach is a technique of the String object, it must be invoked thru a specific 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 technique 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 carries the Unicode Normalization Form as specific by the form_type parameter.

Note

The normalize() method does not alternate the fee of the unique string.

Example

Let’s take a look at an example of how to use the normalize() technique 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 referred to as totn_string1 and totn_string2 that incorporate accent values that appear the same however are extraordinary Unicode values. We have then tested for equality on the unique string values as well as their normalized values.

We have written the output to the net browser console log, for demonstration purposes, to exhibit 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 show the identical text, even though their accent values are specific Unicode values. When we check for equality on these string values the use of the === operator, false is lower back because they are now not equal.

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