Q:

Javascript define string constants as shorthand properties

Is there any way to define a string value like a shorthand property, something like e.g. (this doesn't work):
const dict = {
    USER_LOGIN,
    USER_LOGOUT
};

Which will be equivalent to:
const dict = {
    USER_LOGIN: "USER_LOGIN",
    USER_LOGOUT: "USER_LOGOUT"
};

I want to define a constants dictionary, but I was wondering if I can somehow avoid the repetition pattern MYVALUE : "MYVALUE".
Is there any shorthand way of declaring object keys with values equivalent to their string values, similar to the (not working) code above?

A:

There's no built-in way to do something like that automatically, but if you want to keep the code DRY, you can make a helper function that, when passed an array of strings, creates an object with those properties:

const makeDict = arr => arr.reduce((a, str) => ({ ...a, [str]: str }), {});
const dict = makeDict(['USER_LOGIN', 'USER_LOGOUT']);
console.log(dict);

A:

Shorthand property notation will only work when you have a variable having the same name as the property you want to declare:

const USER_LOGIN = 'USER_LOGIN';
const USER_LOGOUT = 'USER_LOGOUT';

const dict = {
    USER_LOGIN,
    USER_LOGOUT
};

console.log(dict);

Otherwise, you have to specify the whole object:
const dict = {
    USER_LOGIN: "USER_LOGIN",
    USER_LOGOUT: "USER_LOGOUT"
};

Or create it via a helper as @CertainPerformance mentionned.

