Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

string
javascript

What is the string normalize() method in JavaScript?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The normalize() method returns the Unicode Normalization Form of the string.

In Unicode, each string character has a unique numerical value known as a code point. In some situations, a single character can refer to multiple code points. For example, the character “ñ” is represented by the following:

  1. U+00F1 (single code point).

  2. (U+006E) followed by (U+0303).

let string1 = '\u00F1';
let string2 = '\u006E\u0303';

The normalize() method solves this problem. The string is converted to a normal form where all characters with two or more code points are normalized into one representation. There are two main normalization forms:

  1. Canonical equivalence: The sequences of code points represent the same abstract characters (always sorted in the same way). Examples are NFC and NFD.

  2. The sequences of code points represent the same abstract characters, but they are not treated the same in all applications. Examples are NFKD and NFKC.

Syntax

string.normalize(form)

Parameters

form: An optional parameter that refers to the Unicode Normalization Form. It can take the following values:

  1. NFC: Normalization Form Canonical Composition.

  2. NFD: Normalization Form Canonical Decomposition.

  3. NFKC: Normalization Form Compatibility Composition.

  4. NFKD: Normalization Form Compatibility Decomposition.

Return value

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

Code

Let’s look at the code to understand it further. The string in Unicode form is different, but they correspond to the same character. After Unicode normalization, the compared strings are now the same:

let string1 = '\u00F1';
let string2 = '\u006E\u0303';
// printing string
console.log(`${string1}, ${string2}`);
// let's see if the strings are same or not
console.log("string = ", string1 === string2);
// let's see if the length of strings are same or not
console.log("length =", string1.length === string2.length);
// expected output: false
// normalize
const n1 = string1.normalize('NFC');
const n2 = string2.normalize('NFC');
console.log("after normalizing, ")
console.log(`${n1}, ${n2}`);
// checking string
console.log(n1 === n2);
// checking lengths
console.log(n1.length === n2.length);
let string1 = '\u00F1';
let string2 = '\u006E\u0303';
// printing string
console.log(`${string1}, ${string2}`);
// let's see if the strings are same or not
console.log("string = ", string1 === string2);
// let's see if the length of strings are same or not
console.log("length =", string1.length === string2.length);
// expected output: false
// normalize using NFD
const n1 = string1.normalize('NFD');
const n2 = string2.normalize('NFD');
console.log("after normalizing, ")
console.log(`${n1}, ${n2}`);
// checking string
console.log(n1 === n2);
// checking lengths
console.log(n1.length === n2.length);

RELATED TAGS

string
javascript

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring