Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// Numeric enum — auto-increments from 0 enum Direction { North, // 0 East, // 1 South, // 2 West, // 3 } console.log(Direction.North); // 0 console.log(Direction[0]); // "North" — reverse mapping // Custom start value enum HttpStatus { Ok = 200, Created = 201, BadRequest = 400, Unauthorized = 401, NotFound = 404, ServerError = 500, } function handleResponse(status: HttpStatus): string { switch (status) { case HttpStatus.Ok: return "Success"; case HttpStatus.NotFound: return "Resource not found"; default: return `Status ${status}`; } } // String enum — preferred for readability enum LogLevel { Debug = "DEBUG", Info = "INFO", Warning = "WARNING", Error = "ERROR", } function log(level: LogLevel, message: string): void { console.log(`[${level}] ${message}`); } log(LogLevel.Info, "Server started"); log(LogLevel.Error, "Unhandled exception"); // Enum as a type function isError(level: LogLevel): boolean { return level === LogLevel.Error; } console.log(isError(LogLevel.Warning)); // false
Result
Open