# Why use enum in javascript?

**URL:** https://discussions.unity.com/t/why-use-enum-in-javascript/417778
**Category:** Scripting
**Tags:** Scripting
**Created:** [July 10, 2010, 4:41pm UTC](https://discussions.unity.com/t/why-use-enum-in-javascript/417778 "2010-07-10T16:41:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![leegod](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@leegod](https://discussions.unity.com/u/leegod)
#### Post date: [July 10, 2010, 4:41pm UTC](https://discussions.unity.com/t/why-use-enum-in-javascript/417778/1 "2010-07-10T16:41:06Z")

</div>

Why use enum in javascript and how it works.?

I saw some code which use enum at unity-javascript.

---

<div class="post-metadata">

### Author: ![dart](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dart/32/243762_2.png) [@dart](https://discussions.unity.com/u/dart)
#### Post date: [July 10, 2010, 5:29pm UTC](https://discussions.unity.com/t/why-use-enum-in-javascript/417778/2 "2010-07-10T17:29:19Z")

</div>

You use enums to avoid magic numbers [Magic number (programming) - Wikipedia](http://en.wikipedia.org/wiki/Magic_number_%28programming%29), to avoid “an unnamed or ill-documented numerical constant value”.

```auto
enum Commands {patroll, stay, run};

var command : Commands = Commands.stay;

function Update() 
{

	if (Input.GetKey("p"))
	{
		command = Commands.patroll;
	}

	// ...
         
	switch (command)
	{
		case Commands.stay: 
			print("Stay state");  
		break;
			
		case Commands.patroll: 
			print("Patroll state"); 
		break;
			
		case Commands.run:
			print("Run state");
		break;
				
		default:
			print("No command");
		break;
	}
}

```

You could associate stay to 0, patroll to 1 and Run to 2, but it makes your code clearer if you use enums.

---

<div class="post-metadata">

### Author: ![leegod](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@leegod](https://discussions.unity.com/u/leegod)
#### Post date: [July 11, 2010, 1:14am UTC](https://discussions.unity.com/t/why-use-enum-in-javascript/417778/3 "2010-07-11T01:14:57Z")

</div>

Thanks~~

---

<div class="post-metadata">

### Author: ![Jessy](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jessy/32/465975_2.png) [@Jessy](https://discussions.unity.com/u/Jessy)
#### Post date: [July 11, 2010, 2:15am UTC](https://discussions.unity.com/t/why-use-enum-in-javascript/417778/4 "2010-07-11T02:15:20Z")

</div>

I also like them because you get a nice drop-down picker in the Editor.

---

<div class="post-metadata">

### Author: ![goodhustle](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/goodhustle/32/280488_2.png) [@goodhustle](https://discussions.unity.com/u/goodhustle)
#### Post date: [July 11, 2010, 4:06pm UTC](https://discussions.unity.com/t/why-use-enum-in-javascript/417778/5 "2010-07-11T16:06:29Z")

</div>

Yup, what Jesse said. The one caveat I’d add is that although they convert between int and enum pretty nicely, things get a little bit hairy if you have to concatenate arrays of them as strings… that conversion takes them into the named string representation of the enum and not their base integer index. Like say, for example, saving an array of CSV enums or enums in JSON to PlayerPrefs. You’ll probably never run into that issue, but it’ll save you time to know it exists. 🙂
