0
0
Typescriptprogramming~15 mins

Enum member access in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Enum member access
📖 Scenario: You are creating a simple program to manage directions in a navigation app.
🎯 Goal: You will create an enum for directions and access its members to display a chosen direction.
📋 What You'll Learn
Create an enum called Direction with members Up, Down, Left, and Right assigned to 1, 2, 3, and 4 respectively.
Create a variable called chosenDirection and assign it the enum member Direction.Left.
Create a variable called directionName that gets the name of the enum member from chosenDirection.
Print the value of chosenDirection and directionName.
💡 Why This Matters
🌍 Real World
Enums help represent fixed sets of related values like directions, statuses, or categories in apps.
💼 Career
Understanding enums is important for writing clear and maintainable code in TypeScript, common in web development jobs.
Progress0 / 4 steps
1
Create the Direction enum
Create an enum called Direction with members Up = 1, Down = 2, Left = 3, and Right = 4.
Typescript
Need a hint?

Use the enum keyword followed by the name Direction. Assign numbers to each member.

2
Assign an enum member to a variable
Create a variable called chosenDirection and assign it the enum member Direction.Left.
Typescript
Need a hint?

Use let chosenDirection = Direction.Left; to assign the enum member.

3
Get the name of the enum member
Create a variable called directionName that gets the name of the enum member from chosenDirection using the enum reverse mapping.
Typescript
Need a hint?

Use the enum as an indexer: Direction[chosenDirection] to get the name.

4
Print the enum value and name
Print the value of chosenDirection and the value of directionName using console.log.
Typescript
Need a hint?

Use console.log(chosenDirection); and console.log(directionName); to print the values.