Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Intl Segementer() Constructor

JavaScript Intl Segementer() Constructor

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 neveropen";
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
  
neveropen

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments