# \[Extension\] Enums in blocks!

**URL:** <https://forum.makecode.com/t/extension-enums-in-blocks/19170>\
**Category:** Show & Tell\
**Tags:** extension\
**Created:** [April 13, 2023, 8:38pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170 "2023-04-13T20:38:59Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [April 13, 2023, 8:38pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/1 "2023-04-13T20:38:59Z")

</div>

Ever wanted to use Enumerations in blocks? No? Well, now you can!

* * *

## Once again introducing an extension, this time it’s enums!

I was working on an extension and realized how useful enumerations would be to use in blocks. So I put this extension together in about 10 minutes.

> **[GitHub - sargedev/enums: JavaScript / TS Enums in block form](https://github.com/sargedev/enums)**
>
> JavaScript / TS Enums in block form

There’s not a lot to it, there’s a single block and it’s used to create members.  
If you wish to use enums in a method, set the argument type to `number`.

 ![The one single block](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/591bb565d23bde54a8ec849de25316b31e0fe4c8.png)

Wow, such block!

---

<div class="post-metadata">

**Author:** ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)\
**Post date:** [April 13, 2023, 11:18pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/2 "2023-04-13T23:18:55Z")

</div>

what is an enumeration?

---

<div class="post-metadata">

**Author:** ![BoostedGames](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/boostedgames/32/29419_2.png) [@BoostedGames](https://forum.makecode.com/u/BoostedGames)\
**Post date:** [April 14, 2023, 4:44pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/3 "2023-04-14T16:44:53Z")

</div>

According to Google (this will sound very professional)

It is a noun meaning “the action of mentioning a number of things at once.”  
Formally it means “the action of establishing the number of something.”

---

<div class="post-metadata">

**Author:** ![isoffline](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/isoffline/32/8791_2.png) [@isoffline](https://forum.makecode.com/u/isoffline)\
**Post date:** [April 14, 2023, 4:45pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/4 "2023-04-14T16:45:03Z")

</div>

yes great extenstion @randomuser i dont know how to explain it

---

<div class="post-metadata">

**Author:** ![SoftTalker](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/softtalker/32/10776_2.png) [@SoftTalker](https://forum.makecode.com/u/SoftTalker)\
**Post date:** [April 14, 2023, 4:45pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/5 "2023-04-14T16:45:45Z")

</div>

its technicaly a list  
but its for extensions

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [April 14, 2023, 4:54pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/6 "2023-04-14T16:54:29Z")

</div>

Not exactly, It can be applied anywhere

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [April 14, 2023, 4:55pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/7 "2023-04-14T16:55:12Z")

</div>

Oh, that’s kind of like a list of numbers, where you can assign every number to a name.  
For example, writing an enumeration looks something like this:  
(javascript)

```auto
enum MyEnum {
// Name, Value (optional)
    Dog = 1,
    Cat = 2
    ...
}

```

This means that for constants, you can replace them with Enums to make them look tidier.  
For example, say you want to choose between a direction, ‘left’ or ‘right’.

Now, the initial idea may be to use `strings` or numbers.  
(left = “left” or 0, and right = “right” or 1)

```auto
if (direction == "left") {
    ...
}

```

or

```auto
if (direction == 0) {
    ...
}

```

* * *

But instead, you can define enums to make this a lot nicer.

```auto
enum Direction {
    Left, // enums are incremental, so this will be automatically set to 0
    Right // the next member will be +1, so 1
}

if (direction == Direction.Left) {
    ...
}

```

It’s also important to mention that members of enumerations are, well - numbers. Each member is assigned a value, which is auto-increment, or you can define values yourself.

```auto
enum MyEnum {
    Foo, // 0
    Bar // 1
}

console.log(MyEnum.Foo); // Expected output: 0
console.log(MyEnum.Bar == 1); // Expected output: true

```

P.S, all members must adhere to one scheme. If you’re defining a member with an explicit value (eg. `Foo = 2`), all other members must have it defined too. You cannot have a mix of implicit and explicit values in you enumerations.

You might also see expressions like `Foo = 2 << n;` within enums. These are just powers of 2, so the numbers of the members will be exponential. The `<<` operation is a bitwise shift to the left, which means it shifts all of the bits left in a number (eg. 0010 \<\< 1 = 0100)

```auto
enum MyEnum {
    Foo = 2 << 0, // 2
    Bar = 2 << 1, // 4
    Baz = 2 << 2 // 8
    ...
}

```

With my extension, all of the members are stored in one built-in enumeration, but you can add your own members. I hope this cleared something up, if you have more questions just ask!

**Further reading / reference**

> **[Handbook - Enums](https://www.typescriptlang.org/docs/handbook/enums.html)**
>
> How TypeScript enums work

> **[TypeScript Enums](https://www.w3schools.com/typescript/typescript_enums.php)**
>
> An enum is a special 'class' that represents a group of constants (unchangeable variables).

---

<div class="post-metadata">

**Author:** ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)\
**Post date:** [April 14, 2023, 5:11pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/8 "2023-04-14T17:11:42Z")

</div>

Yes thank you!

---

<div class="post-metadata">

**Author:** ![isoffline](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/isoffline/32/8791_2.png) [@isoffline](https://forum.makecode.com/u/isoffline)\
**Post date:** [April 14, 2023, 5:14pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/9 "2023-04-14T17:14:44Z")

</div>

ahhh yes some code i dont understand

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [April 14, 2023, 5:15pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/10 "2023-04-14T17:15:08Z")

</div>

Glad this post help you understand this more!  
I made this extension mostly for myself, since I needed enums for something I was making so I decided to share it anyway.

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [April 14, 2023, 5:30pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/11 "2023-04-14T17:30:18Z")

</div>

Do you want me to explain something further? I’d be glad to if it helps!

---

<div class="post-metadata">

**Author:** ![isoffline](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/isoffline/32/8791_2.png) [@isoffline](https://forum.makecode.com/u/isoffline)\
**Post date:** [April 17, 2023, 4:47pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/12 "2023-04-17T16:47:47Z")

</div>

nah that was sarcasm i can understand it

---

<div class="post-metadata">

**Author:** ![ChimbroDaPro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/chimbrodapro/32/15852_2.png) [@ChimbroDaPro](https://forum.makecode.com/u/ChimbroDaPro)\
**Post date:** [December 8, 2023, 9:18pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/13 "2023-12-08T21:18:56Z")

</div>

Bro is literally a teacher

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [December 8, 2023, 10:12pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/14 "2023-12-08T22:12:36Z")

</div>

That’s what I partially try to do here!

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [May 14, 2024, 9:20pm UTC](https://forum.makecode.com/t/extension-enums-in-blocks/19170/15 "2024-05-14T21:20:22Z")

</div>

## Imporant announcement

I changed my GitHub username, so please remove this extension in any project you may have it imported, and instead import the new link under my new username (sargedev).

This is not necessary, as [the extension should still work](https://forum.makecode.com/t/how-does-makecode-handle-github-username-migrations/28716/11), but it would be the safest option. By importing the new link you will also get access to any new updates.

**WARNING: Do not import the new link before removing the old one, as this could cause conflicts**

> **[GitHub - sargedev/enums: JavaScript / TS Enums in block form](https://github.com/sargedev/enums)**
>
> JavaScript / TS Enums in block form
