How to Iterate over Enum Values in TypeScript? Last Updated : 25 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Enums in TypeScript are a way to define a set of named constants. Sometimes we may need to iterate over all the values of an enum. There are several approaches to iterate over the enum values in TypeScript which are as follows: Table of Content Using a for...in loopUsing Object.values()Using a for…of loopUsing Object.keys()Examples of Iterating over Enum Values in TypeScriptUsing a for...in loopIn this approach, we are using a for...in loop to iterate over the keys of the enum and access the corresponding values. It is important to check if the property is a key of the enum to avoid iterating over any prototype properties. Syntax:for (const key in Enum) { // Use 'value' as needed}Example: The below code uses for...in loop to iterate over enum values in typescript JavaScript enum StudentInfo { Name = "Yogesh", RollNo = "14", Branch = "Civil", Div = "A", } for (const key in StudentInfo) { if (StudentInfo.hasOwnProperty(key)) { console.log(StudentInfo[key]); } } Output: Yogesh14CivilAUsing Object.values()In this approach, we will be using the Object.values() method. This method is used to extract the values of the enum. It returns an array containing the enum values, which can then be iterated over using array methods or loops. Syntax:const values = Object.values(YourEnum);values.forEach(value => { // Use 'value' as needed});Example: This example uses Object.values() to iterate over enum values in typescript. JavaScript enum GFG { Name = "GeeksForGeeks", Founder = "Sandeep Jain", Est = "2008" } const values = Object .values(GFG); values .forEach(value => console.log(value)); Output: GeeksForGeeksSandeep Jain2008Using a for…of loopThe for…of loop offers a streamlined approach to directly iterate over the values of an enum without the need for additional property checks. This loop provides a concise syntax for traversing enum values, enhancing code readability and maintainability. Syntax:for (const value of YourEnum) { // Use 'value' as needed}Example: JavaScript enum Direction { North = "N", South = "S", East = "E", West = "W", } for (const value of Object.values(Direction)) { console.log(value); } Output: NSEWUsing Object.keys()In this approach, we use the Object.keys() method. This method retrieves the keys of the enum as strings, which can then be mapped to their corresponding values. Syntax:const keys = Object.keys(YourEnum);keys.forEach(key => { // Use 'YourEnum[key as keyof typeof YourEnum]' as needed});Example: JavaScript enum Colors { Red = "RED", Green = "GREEN", Blue = "BLUE" } const keys = Object.keys(Colors); keys.forEach(key => { console.log(Colors[key as keyof typeof Colors]); }); Output: REDGREENBLUE Comment More infoAdvertise with us Next Article How to Iterate over Enum Values in TypeScript? G ghuleyogesh Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads How to Iterate over Map Elements in TypeScript ? In TypeScript, iterating over the Map elements means accessing and traversing over the key-value pairs of the Map Data Structure. The Map is nothing but the iterative interface in TypeScript. We can iterate over the Map elements in TypeScript using various approaches that include inbuilt methods and 4 min read How to Access Enum Values in TypeScript ? Enums are a feature in TypeScript that help organize collections of related values. Accessing these values efficiently is crucial for clean, maintainable code. This guide provides a straightforward overview of different methods to access enum values in TypeScript, including bracket notation, dot not 3 min read How to Get an Object Value By Key in TypeScript In TypeScript, we can get an object value by key by accessing the specific properties within the objects of the dynamic type. This can be done using Dot Notation, Bracket Notation, and Optional Chaining. In this article, we will explore all these approaches along with their implementation in terms o 5 min read How to Create an Enum With String Values in TypeScript ? To create an enum with string values in TypesScript, we have different approaches. In this article, we are going to learn how to create an enum with string values in TypesScript. Below are the approaches used to create an enum with string values in TypesScript: Table of Content Approach 1: Using Enu 3 min read How to Iterate Array of Objects in TypeScript ? In TypeScript, we can iterate over the array of objects using various inbuilt loops and higher-order functions, We can use for...of Loop, forEach method, and map method. There are several approaches in TypeScript to iterate over the array of objects which are as follows:Table of ContentUsing for...o 3 min read How to Extract Interface Members in TypeScript ? In TypeScript, you can extract interface members (properties and methods) from a class using several approaches. we are going to learn how to extract interface members in TypeScript. Below are the approaches used to extract interface members in TypeScript: Table of Content Manual Extractionimplement 3 min read How to check interface type in TypeScript ? Typescript is a pure object-oriented programming language that consists of classes, interfaces, inheritance, etc. It is strict and it statically typed like Java. Interfaces are used to define contacts in typescript. In general, it defines the specifications of an entity. Below is an example of an in 2 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 How to Iterate Over Object Properties in TypeScript In TypeScript, Objects are the fundamental data structures that use key-value pair structures to store the data efficiently. To iterate over them is a common task for manipulating or accessing the stored data. TypeScript is a superset of JavaScript and provides several ways to iterate over object pr 3 min read How to Cast Object to Interface in TypeScript ? In TypeScript, sometimes you need to cast an object into an interface to perform some tasks. There are many ways available in TypeScript that can be used to cast an object into an interface as listed below: Table of Content Using the angle bracket syntaxUsing the as keywordUsing the spread operatorU 3 min read Like