The Wayback Machine - https://web.archive.org/web/20250115223252/https://www.geeksforgeeks.org/javascript-string-normalize-method/
Open In App

JavaScript String normalize() Method

Last Updated : 11 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript String normalize() method returns a Unicode normalization form of a given input string. If the given input is not a string, then at first it will be converted into a string then this method will work.

Syntax

string.normalize([form])

Parameters

Here the parameter is formed which is of many types- 

  • NFC: Normalization Form Canonical Composition.
  • NFD: Normalization Form Canonical Decomposition.
  • NFKC: Normalization Form Compatibility Composition.
  • NFKD: Normalization Form Compatibility Decomposition.

These all say the Unicode Normalization Form. 

Return value

It returns a new string containing the Unicode Normalization Form of the given input string.

Example 1: This example shows the basic use of the string.normalize() method in Javascript.

javascript
let a = "Geeks For Geeks";

b = a.normalize('NFC')
c = a.normalize('NFD')
d = a.normalize('NFKC')
e = a.normalize('NFKD')

console.log(b, c, d, e); 

Output:  

Geeks For GeeksGeeks For GeeksGeeks For GeeksGeeks For Geeks

Example 2: This example shows the basic use of the string.normalize() method in Javascript.

javascript
// Taking a string as input.
let a = "GeeksForGeeks";

// calling normalize method.
b = a.normalize('NFC')
c = a.normalize('NFD')
d = a.normalize('NFKC')
e = a.normalize('NFKD')

// Printing normalised form.
console.log(b);
console.log(c);
console.log(d);
console.log(e);

Output: 

GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks

Supported Browsers

  • Google Chrome 34
  • Edge 12
  • Firefox 31
  • Opera 21
  • Safari 10

JavaScript String normalize() Method – FAQs

What is the normalize() method in JavaScript?

The normalize() method in JavaScript is used to convert a string into a normalized form, following a specified Unicode normalization form. This is useful for comparing and manipulating strings that may contain different Unicode representations of the same characters.

What happens if the form argument is not specified?

If the form argument is not specified, the normalize() method defaults to “NFC” (Normalization Form Canonical Composition).

Can normalize() handle non-string types?

normalize() is intended for use with strings. If called on a non-string type, JavaScript will first convert the value to a string if possible. However, calling it on null or undefined will throw an error.

Does the normalize() method modify the original string?

No, the normalize() method does not modify the original string. It returns a new string that is the normalized form of the original string.

What are some common use cases for the normalize() method?

Common use cases for the normalize() method include:

  • Ensuring consistent string comparison, especially for strings containing Unicode characters.
  • Preparing strings for storage and retrieval in a standardized format.
  • Comparing user input to database values that may have different Unicode representations.
  • Processing text data that contains accents and special characters.

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!


Next Article

Similar Reads

three90RightbarBannerImg