Challenge - 5 Problems
Const Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of const enum in compiled JavaScript
What is the output of the following TypeScript code when compiled to JavaScript and run?
Typescript
const enum Colors { Red = 1, Green, Blue } console.log(Colors.Green);
Attempts:
2 left
💡 Hint
Remember that const enums are inlined during compilation.
✗ Incorrect
Const enums are replaced by their values during compilation. Colors.Green is 2 because Red starts at 1 and Green increments by 1.
🧠 Conceptual
intermediate2:00remaining
Why use const enums for optimization?
Which of the following best explains why const enums improve runtime performance in TypeScript?
Attempts:
2 left
💡 Hint
Think about what happens to enum references in the compiled code.
✗ Incorrect
Const enums are replaced by their literal values during compilation, so no enum object is created and no lookup is needed at runtime.
❓ Predict Output
advanced2:00remaining
Effect of const enum on emitted JavaScript code
Given this TypeScript code, what will the compiled JavaScript output be?
Typescript
const enum Status { Success = 0, Failure = 1 } function check(status: Status) { if (status === Status.Success) { return "OK"; } return "Error"; } console.log(check(Status.Failure));
Attempts:
2 left
💡 Hint
Const enums are replaced by their values, so no enum object exists at runtime.
✗ Incorrect
The function compares the input to 0 (Success) and returns "OK" if matched, else "Error". Status.Failure is replaced by 1, so the output is "Error".
🔧 Debug
advanced2:00remaining
Why does this code cause a runtime error?
Consider this TypeScript code snippet:
const enum Directions {
Up,
Down,
Left,
Right
}
console.log(Directions);
Why does this cause a runtime error when compiled and run?
Attempts:
2 left
💡 Hint
Think about what happens to const enums in the compiled JavaScript.
✗ Incorrect
Const enums are completely removed during compilation and replaced by their values. The Directions object does not exist at runtime, so logging it causes a ReferenceError.
🚀 Application
expert3:00remaining
Optimizing large enum usage with const enums
You have a large enum used only for numeric constants in performance-critical code. Which approach best optimizes runtime speed and reduces output size?
Attempts:
2 left
💡 Hint
Inlining values removes object lookups and reduces code size.
✗ Incorrect
Const enums inline their values at compile time, eliminating runtime object lookups and reducing code size, which improves performance in critical code.