Menu
Coddy logo textTech

Using String Enums

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 54 of 73.

String enums work particularly well as function parameters because they provide clear, readable values while maintaining type safety.

Here's how you can use a string enum in a function:

function move(direction: Direction): void {
  console.log(`Moving ${direction.toLowerCase()}...`);
}

When you call this function, TypeScript will only accept the predefined enum values. You can call it using move(Direction.Up), which will output "Moving up...". Your code editor will provide autocompletion for the available enum members, and TypeScript will catch any attempts to pass invalid direction values.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You are provided with the following from the previous challenge:

  • The Direction string enum with Up, Down, Left, and Right members
  • Variables: upDirection, downDirection, leftDirection, and rightDirection

Create a function named move that takes one parameter:

  • direction of type Direction

The function should:

  • Print a message in the format "Moving [direction]..." where [direction] is the lowercase version of the enum's string value
  • Have a return type of void

Create a function named getMovementDescription that takes one parameter:

  • direction of type Direction

The function should:

  • Return a string describing the movement based on the direction:
  • For Direction.Up: return "Going upward"
  • For Direction.Down: return "Going downward"
  • For Direction.Left: return "Turning left"
  • For Direction.Right: return "Turning right"
  • Have an explicit return type of string

Create a variable named playerDirection of type Direction and assign it Direction.Up.

Print the following outputs:

  • Call move with Direction.Up
  • Call move with leftDirection
  • Call move with playerDirection
  • Print the result of calling getMovementDescription with Direction.Down
  • Print the result of calling getMovementDescription with rightDirection
  • Call move with Direction.Right

Cheat sheet

String enums work well as function parameters, providing clear, readable values with type safety.

Use string enums in function parameters:

function move(direction: Direction): void {
  console.log(`Moving ${direction.toLowerCase()}...`);
}

TypeScript will only accept predefined enum values and provide autocompletion for available enum members.

Try it yourself

// From previous challenge - Direction enum and variables
enum Direction {
    Up = "Up",
    Down = "Down",
    Left = "Left",
    Right = "Right"
}

const upDirection = Direction.Up;
const downDirection = Direction.Down;
const leftDirection = Direction.Left;
const rightDirection = Direction.Right;

// TODO: Write your code here
// Create the move function that takes a Direction parameter and prints "Moving [direction]..."
// Create the getMovementDescription function that returns movement descriptions
// Create the playerDirection variable
// Call the functions and print the required outputs
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To TypeScript