JavaScript Enum: The Unofficial Guide to Emulating Enums

4 Min Read

Ahoy! Let’s Talk About JavaScript Enum!

So, you’re coding away in JavaScript, minding your own business, when suddenly you find yourself wishing for enums. You remember those good ol’ days in other languages, where you could create a nice, clean enumeration to store a collection of related values. You chuckle, knowing that in JavaScript, there’s no such thing as an official enum. But worry not, fearless coder! Today, we’ll explore some cheeky ways to emulate JavaScript enum and bring a bit of that structured joy into your life. Let’s dive in!

JavaScript Enum, the “Unofficial” Version

Since JavaScript is a dynamic language, we can easily create a “faux enum” using an object. For example, let’s say you’re building a game, and you want an enumeration to represent different enemy types. Here’s one way to create a JavaScript enum:

const EnemyType = {
    ZOMBIE: 'zombie',
    ALIEN: 'alien',
    ROBOT: 'robot',

// Usage:
const currentEnemy = EnemyType.ZOMBIE;

Voila! You’ve got yourself a makeshift JavaScript enum! You can now use EnemyType to represent the various enemy types in your game. It’s not quite the same as a proper enum, but it gets the job done.

Freeze It Like an Ice Cream on a Hot Summer Day

Now, one caveat with our JavaScript enum wannabe is that it’s mutable. You or someone else could accidentally modify it, and suddenly you have a new enemy type called “Potato.” (Although, that could be an interesting game idea.) To prevent such catastrophes, we can use Object.freeze() to make our enum immutable:

const EnemyType = Object.freeze({
    ZOMBIE: 'zombie',
    ALIEN: 'alien',
    ROBOT: 'robot',

Now our enum is as unchangeable as the laws of gravity, or as my grandma’s stubbornness when it comes to her secret cookie recipe. Nobody’s getting their hands on that!

For the Numeric Lovers Out There

But wait, there’s more! What if you prefer numeric enums, like the good ol’ days of C# or Java? No problem! Just use numbers instead of strings for your enum values:

const EnemyType = Object.freeze({
    ZOMBIE: 0,
    ALIEN: 1,
    ROBOT: 2,

Boom! Now you’ve got numeric enums that you can flaunt around like the proud JavaScript coder you are.

Conclusion: Enums, Schmenums, and a Little Bit of Fun

While JavaScript doesn’t officially support enums, that shouldn’t stop you from emulating them with a bit of creative coding and some good humor. As we’ve shown, you can create your own faux enums using objects and the Object.freeze() method. Whether you prefer string-based or numeric enums, JavaScript has got your back, and so do we!

So, the next time you find yourself longing for the structure and organization of enums in your JavaScript projects, remember this guide and these nifty tricks. With a pinch of creativity and a dash of coding know-how, you can bring some enum-like functionality into your projects and continue to have a laugh as you code away.

A Few More Laughs Before You Go

Just for fun, let’s imagine a couple more humorous enums:

const SandwichFillings = Object.freeze({
    TUNA: 'tuna',
    EGG: 'egg',
    HAM: 'ham',
    SURPRISE: 'mystery meat',

const CoffeeSize = Object.freeze({
    SMALL: '8oz',
    MEDIUM: '12oz',
    LARGE: '16oz',
    BUCKET: '64oz',

Happy coding, and remember to have some fun with your JavaScript enums!

Share this Article
Leave a comment