Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
Iterators & Generators in TypeScript
Next article icon

TypeScript Array Symbol.iterator Method

Last Updated : 03 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In TypeScript the Symbol.iterator function plays a role, in allowing iteration over arrays using for...of loops and other iterator-based mechanisms. This built-in function eliminates the need, for definition when working with arrays simplifying the process of accessing elements.

Syntax:

const iterator: Iterator<T> = array[Symbol.iterator]();

Parameter:

  • None: No parameter is required.

Return Value:

  • It returns the iterable point that can be accessed in a for loop for getting values of that array.

Example 1: We will iterate each element of an array by using a, for...of loop and see the working of Symbol.iterator Method.

JavaScript
// TypeScript code with annotations let numbers: number[] = [1, 2, 3, 4, 5,     6, 7, 8, 9, 10];  // Here we use Symbol.iterator to get the default iterator let iterator = numbers[Symbol.iterator]();  for (let num of iterator) {     console.log(num); } 

Output:

1
2
3
4
5
6
7
8
9
10

Example 2: We are going to manually iterate using the next method and see the working of Symbol.iterator Method.

JavaScript
// TypeScript code with annotations let geeks: string[] = ['Pankaj', 'Ram', 'Shravan', 'Jeetu'];  // Here we use Symbol.iterator to get the default iterator let iterator = geeks[Symbol.iterator]();  // Manually iterating using the next method let output = iterator.next();  while (!output.done) {     console.log(output.value);     output = iterator.next(); } 

Output:

Pankaj
Ram
Shravan
Jeetu

Next Article
Iterators & Generators in TypeScript
author
pankajbind
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • TypeScript

Similar Reads

  • TypeScript Array entries() Method
    The Array.prototype.entries() method in TypeScript returns a new array iterator object that contains the key/value pairs for each index in the array. This method is useful when you need to iterate over the key/value pairs in the array. Note: In TypeScript, the entries() method is not available direc
    3 min read
  • TypeScript Array lastIndexOf() Method
    The Array.prototype.lastIndexOf() method in TypeScript is used to find the last index at which a given element can be found in the array, searching backwards from the fromIndex. It returns the index of the found element, or -1 if the element is not present. Syntax: array.lastIndexOf(searchElement[,
    2 min read
  • Iterators & Generators in TypeScript
    Iterators and generators are powerful features in TypeScript (and JavaScript) that allow for the creation and manipulation of sequences of values in a lazy, efficient manner, in this article we shall give a detailed account of what these concepts are all about and how to implement them in TypeScript
    3 min read
  • JavaScript Array Iteration Methods
    JavaScript Array iteration methods perform some operation on each element of an array. Array iteration means accessing each element of an array. There are some examples of Array iteration methods are given below: Using Array forEach() MethodUsing Array some() MethodUsing Array map() MethodMethod 1:
    3 min read
  • Iterate Over Characters of a String in TypeScript
    Iterating over characters of a string involves going through them one by one using loops or specific methods. This is useful for tasks like changing or analyzing the content of a string efficiently. Example:Input: string = "Hello Geeks"; Output: H e l l o G e e k sBelow listed methods can be used to
    4 min read
  • PHP | ArrayIterator current() Function
    The ArrayIterator::current() function is an inbuilt function in PHP which is used to return the current element of array iterator. Syntax: mixed ArrayIterator::current( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the current element of array.
    1 min read
  • PHP | ArrayIterator next() Function
    The ArrayIterator::next() function is an inbuilt function in PHP which is used to move the iterator to the next entry. Syntax: void ArrayIterator::next( void ) Parameters: This function does not accept any parameters. Return Value: This function does not return any value. Below programs illustrate t
    1 min read
  • Underscore.js _.iterators.accumulate() method
    With the help of _.iterators.accumulate(iter, binaryFn) method, we can get the new iterator function which when called will iterate the next step with iter and generate the value by using binaryFn. Syntax:  _.iterators.accumulate(iter, binaryFn) Return: Return the iterator function which will genera
    1 min read
  • How to Iterate Over Characters of a String in TypeScript ?
    In Typescript, iterating over the characters of the string can be done using various approaches. We can use loops, split() method approach to iterate over the characters. Below are the possible approaches: Table of Content Using for LoopUsing forEach() methodUsing split() methodUsing for...of LoopUs
    2 min read
  • 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 @@it
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences