Thursday, October 23, 2025
HomeLanguagesJavascriptJavascript String @@iterator Method

Javascript String @@iterator Method

String [@@iterator]( ) Method is used to make String iterable. [@@iterator]() returns an iterator object which iterates over all code points of String. String[@@iterator] is a Built-in Property of String. 

We can use this method by making a string iterator. We can make an iterator by calling the @@iterator property of String. In place of @@iterator, we use Symbol.iterator constant.

Syntax:

// Test String
var str ="String"; 

// iterator to String
var iter = str[Symbol.iterator]();

We can get the iterator object with next(). It returns the object with the key value and is done. The value key holds a real iterating value string and the done key holds true or false if iteration finishes it keeps true if not then false. We can get the value of the object by str.value and done by str.done.

Example 1: Below is the code that illustrates the use of the above approach.

Javascript




<script>
  const str = 'GFG';
  const iterator = str[Symbol.iterator]();
  let theChar = iterator.next();
  
  for(let i = 0; i < str.length ;i++) {
    console.log(theChar.value , theChar.done);
    theChar = iterator.next();
  }
</script>


Output : 

"G" false
"F" false
"G" false

We can use the @@iterator method without assigning an iterator by using for — of loop to iterate the over-collection of data in using @@iterator method. On each iteration for — of loop call _next().value to iterate in the collection.

Example 2: Below is the code that illustrates the use of the above approach.

Javascript




<script>
  const str = 'GFG';
  const iterator = str[Symbol.iterator]();
  let theChar = iterator;
  
  for(let i = 0; i < str.length ;i++)
  {
      console.log(theChar.next().value );
  }
  
  // Using for - of loop
  console.log("Iterating by for-of loop :")
  for(let i of str)
  {
      console.log(i)
  }
</script>


Output:

"G"
"F"
"G"
Iterating by for-of loop  :
"G"
"F"
"G"

Supported Browsers:

  • Google Chrome 38 and above
  • Edge 12 and above
  • Firefox 36 and above
  • Opera 25 and above
  • Safari 9 and above
  • Internet Explorer not supported

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete 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

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS