Open In App

JavaScript Intl Segementer() Constructor

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Intl Segmenter() Constructor is used for creating Intl.Segmenter object. This constructor is created using the new keyword. If we create the constructor without the new keyword it will give a TypeError.

Syntax:

new Intl.Segmenter(loc, opt)

Parameters: It has two parameters both are optional.

  • loc: It is a String or an array of Strings that contains the general form and interpretation of arguments
  • opt: It is an object which contains properties like localeMatcher and granularity.

Return Value: An Intl.Segmenter object

Below examples illustrate the JavaScript Intl Segmenter() Constructor:

Example 1: This example creates a basic segmenter object and uses it to segment a sentence

Javascript




const format = new Intl.Segmenter("en", {granularity:"word"})
  
var sen = "Welcome to GeeksforGeeks";
var iter = format.segment(sen)[Symbol.iterator]();
  
for(var i = 0; i<5;i++) {
    console.log(iter.next().value)
}


Output: The blank line in the output represents the space between the words

Welcome

to
  
GeeksforGeeks

Example 2: This example uses the Segmenter object to segment the sentence into individual letters.

Javascript




const format = new Intl.Segmenter("en", {granularity:"grapheme"})
  
var sen = "Geeks";
var iter = format.segment(sen)[Symbol.iterator]();
  
for(var i = 0; i<5;i++) {
    console.log(iter.next().value.segment)
}


Output:

G
e
e
k
s

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

We have a complete list of JavaScript Intl methods to check please go through, the JavaScript Intl Reference article


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads