'how to create enum values with es6 in node js?

Is it possible to create enum values in JavaScript and assign them to integer values, similar to other languages. For example in C#, I declare an enum in the following way:

enum WeekDays
{
    Monday = 0,
    Tuesday =1,
    Wednesday = 2,
    Thursday = 3,
    Friday = 4,
    Saturday =5,
    Sunday = 6
}


Solution 1:[1]

You can use object as an enum after freezing it like example below:

const WeekDays = Object.freeze({
  Monday: 0,
  Tuesday: 1,
  Wednesday: 2,
  Thursday: 3,
  Friday: 4,
  Saturday: 5,
  Sunday: 6
})

Solution 2:[2]

You can create a simple object and export it via module.exports:

// days.js
module.exports = {
  Monday: 0,
  Tuesday: 1,
  Wednesday: 2,
  Thursday: 3,
  Friday: 4,
  Saturday: 5,
  Sunday: 6
}

// file-where-you-want-to-use.js
const DAYS = require('./days.js');
console.log(DAYS.Monday);

Solution 3:[3]

You can use a plain old object:

const WeekDays = {
    Monday: 0,
    Tuesday: 1,
    Wednesday: 2,
    Thursday: 3,
    Friday: 4,
    Saturday: 5,
    Sunday: 6
}

const day1 = WeekDays.Monday;
const day2 = WeekDays.Tuesday;

console.log(day1, day2, day1 === day2);
console.log(day1 === WeekDays.Monday, day2 === WeekDays.Tuesday);

Solution 4:[4]

To add some sort of variables that you'd like to use:

// days.js
module.exports = {
  Monday: 0,
  Tuesday: 1,
  Wednesday: 2,
  Thursday: 3,
  Friday: 4,
  Saturday: 5,
  Sunday: 6
}

// file-where-you-want-to-use.js
const DAYS = require('./days.js');
const firstDayOfWeek = 'Monday';

console.log((DAYS)[firstDayOfWeek]);   // 0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fazlu
Solution 2 Eugene Obrezkov
Solution 3 Olian04
Solution 4 Dave Lee