4

Can anyone explain the output i am getting from the below code?

enum days { sun = 1, mon = 0, tues };

console.log(days[1]); // returns tues // expected output -- mon
console.log(days[0]); // returns mon // expected output -- sun

Also, please explain how can i get sun printed in this case? This scenario is baffling me. Request you guys to provide valid explanations only after you have executed the code and observed the output.

2 Answers 2

2

This typescript:

enum days { sun = 1, mon = 0, tues };

compiles to this javascript:

var days;
(function (days) {
    days[days["sun"] = 1] = "sun";
    days[days["mon"] = 0] = "mon";
    days[days["tues"] = 1] = "tues";
})(days || (days = {}));
;

This first part: days[days["sun"] = 1] = "sun";

first evaluates days["sun"] = 1 which:

  • makes sure that you're able to call days.sun and get value 1
  • returns the value set at key "sun" => 1. This means that initially days[1] will be set to "sun".

The second part: days[days["mon"] = 0] = "mon";

  • makes sure that you're able to call days.mon and get value 0
  • returns the value set at key "mon" => 0. So days[0] will be set to "mon".

This third part however: days[days["tues"] = 1] = "tues";

evaluates days["tues"] = 1 which

  • makes sure that you're able to call days.tues and get value 1
  • also returns the value set at key "tues" => 1

This means that at this point days[1] will be overwritten with value "tues"

Sign up to request clarification or add additional context in comments.

4 Comments

This sure answers most of my question.. but i also want to know if there is a way that i can get sun printed here
If you supply tues a unique value (2) or just remove all values, then you can get sun to be printed with days[days.sun]
yes that is fine. provided i do not make changes in the current code, is it possible or has the value been overridden as you pointed out?
The value sun doesn't exist anymore. It is overwritten with tues. Only the key sun still exists.
-1

The way you have constructed your enum is unusual. As per the documentation undefined enums will automatically create a value for themselves by +1 to the previous value.

Your current enum structure will have two values set to 1. Typescript/javascript seems to select the last item with the desired value.

Your code:

enum days{
sun = 1,
mon = 0,
tues
}

VS (a similar enum from a project of mine that works fine)

enum days{
Sun = 0,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat
}

As per documentation (https://www.typescriptlang.org/docs/handbook/enums.html)

It does not have an initializer and the preceding enum member was constant. In this case the value of the current enum member will be the value of the preceding enum member plus one. One exception to this rule is the first element on an enum. If it does not have initializer it is assigned the value 0

7 Comments

We can give the values we want in an enum. Please stick to the question why the behaviour is not as expected?
@HiteshSikka because the way you have declared it makes no sense. You say that at index0 the value is eq 1 then at index1 the value is 0. This means that index2 (tuesday) the value will also be 1. Enums made in this manner create the values automatically based on the last value. as per the documentation: typescriptlang.org/docs/handbook/enums.html ... In this case the value of the current enum member will be the value of the preceding enum member plus one ...
@HiteshSikka At the moment your enum has two value 1s. It looks like typescript/js defualts to the last instance of a value when getting the enum (this is a guess by me)
I am not looking for guesses or what is obvious. Even i know what an enum is!! Its not a simple question. Please give a thought to it. At least think how sun can be printed. Take it as a challenge!!
@HiteshSikka not possible... The value that you have duplicated is how you use it. You would have to re-write how enums work/translate into js to get it to work in the way you want. If you require sun=0, mon=1, tue=0 (strange use case) your best bet is to use a class with preset const values instead of enums. Using normal js you can then get the name of the property as a string (or use a map/dictionary)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.