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:
normalize()This method returns the Unicode Normalization Form of the calling string value using the default normalization form (NFC).
Returns value of type String.
normalize(form)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
form | optional | A 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,
- We define a string variable
strwith a value that contains a character in a composed form and its decomposed equivalent. - We use the
normalize()method with no arguments to normalize the string to the default form. - The result is stored in the variable
normalizedStr. - We log
normalizedStrto the console using theconsole.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,
- We define a string variable
strwith a value that contains a character in composed form. - We use the
normalize()method with the argument'NFD'to normalize the string to the NFD form. - The result is stored in the variable
normalizedStr. - We log
normalizedStrto the console using theconsole.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,
- We define a string variable
strwith a value that contains a character in composed form. - We use the
normalize()method with the argument'NFKC'to normalize the string to the NFKC form. - The result is stored in the variable
normalizedStr. - We log
normalizedStrto the console using theconsole.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.