JavaScript String normalize() Method
The JavaScript String normalize() method is used to retrieve a Unicode normalization form of a given input string. If the input is not a string, it will be converted into one before the method operates. The default normalization form is "NFC", Normalization Form Canonical Composition.
It accepts an optional parameter named 'form' specifies the Unicode normalization form, it can take the following values:
- NFC − Normalization Form Canonical Composition.
- NFD − Normalization Form Canonical Decomposition.
- NFKC − Normalization Form Compatibility Composition.
- NFKD − Normalization Form Compatibility Decomposition.
Syntax
Following is the syntax of JavaScript String normalize() method −
normalize(form)
Parameters
This method accepts a parameter named 'form', which is described below −
- form (optional) − It specifies the Unicode normalization form.
Return value
This method returns a new string containing the Unicode normalization form of the input string.
Example 1
If we omit the form parameter, the method uses the default normalization form "NFC".
In the following example, we are using the JavaScript String normalize() method to retrieve Unicode normalization form of the given string "Tutorials Point". Since we omitted the form parameter, the method uses the default normalization form, which is NFC.
<html>
<head>
<title>JavaScript String normalize() Method</title>
</head>
<body>
<script>
const str = "Tutorials Point";
document.write("The given string: ", str);
//using normalize() method
var new_str = str.normalize();
document.write("<br>The Unicode normalization of the given string: ", new_str);
</script>
</body>
</html>
Output
The above program returns a string containing the Unicode normalization form as −
The given string: Tutorials Point The Unicode normalization of the given string: Tutorials Point
Example 2
When you pass a specific normalization form "NFKC" as an argument to this method, it modifies the string according to that form.
The following is another example of the JavaScript String normalize() method. We use this method to retrieve a Unicode normalization form of the given string "Hello JavaScript" and it modifies the returned string based on the specified form value "NFKC".
<html>
<head>
<title>JavaScript String normalize() Method</title>
</head>
<body>
<script>
const str = "Hello JavaScript";
document.write("The given string: ", str);
const form = "NFKC";
document.write("<br>The form: ", form);
//using the normalize() method
const new_str = str.normalize(form);
document.write("<br>The Unicode normalization of the given string: ", new_str);
</script>
</body>
</html>
Output
After executing the above program, it will return a string containing the Unicode normalization form.
The given string: Hello JavaScript The form: NFKC The Unicode normalization of the given string: Hello JavaScript