Friday, April 20, 2018

Loop through all of the values in an enum in TypeScript.

enum LetterGrade {
   A = 'A',
   B = 'B',
   C = 'C',
   D = 'D',
   F = 'F'
}
 
for (let grade in LetterGrade) {
   console.log(grade);
}

 
 

The above will give us A to F, but what is below will give us 0 to 4 before A to F and then A to F as well.

enum LetterGrade {
   A,
   B,
   C,
   D,
   F
}
 
for (let grade in LetterGrade) {
   console.log(grade);
}

No comments:

Post a Comment