0
0
Typescriptprogramming~20 mins

Const enums and optimization in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Const Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A1
Bundefined
C2
D"Green"
Attempts:
2 left
💡 Hint
Remember that const enums are inlined during compilation.
🧠 Conceptual
intermediate
2:00remaining
Why use const enums for optimization?
Which of the following best explains why const enums improve runtime performance in TypeScript?
AThey generate extra code to check enum values at runtime.
BThey inline enum values, removing object lookups at runtime.
CThey create a separate enum object to reduce memory usage.
DThey delay enum value evaluation until runtime.
Attempts:
2 left
💡 Hint
Think about what happens to enum references in the compiled code.
Predict Output
advanced
2: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));
A"Error"
BReferenceError: Status is not defined
C"Failure"
D"OK"
Attempts:
2 left
💡 Hint
Const enums are replaced by their values, so no enum object exists at runtime.
🔧 Debug
advanced
2: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?
ABecause the enum has duplicate values causing a conflict.
BBecause the enum values are strings, which cannot be logged directly.
CBecause the enum is not exported and cannot be accessed outside its module.
DBecause const enums are removed during compilation and no Directions object exists at runtime.
Attempts:
2 left
💡 Hint
Think about what happens to const enums in the compiled JavaScript.
🚀 Application
expert
3: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?
AUse a const enum so all references are inlined as numeric literals.
BUse a regular enum and access values via the enum object at runtime.
CUse string enums to improve readability and debugging.
DUse a namespace with constant variables instead of enums.
Attempts:
2 left
💡 Hint
Inlining values removes object lookups and reduces code size.