Redux actionCreator question

In the challenges related to Redux, I saw a couple of times the actionCreator written in the form below:

const addToDo = (todo) => {
  return {
    type: 'ADD_TO_DO',
    todo
  }
}

MY QUESTION: how come the “todo” key doesn’t have a value, and the code still works ?

Thank you a lot in advance,
Noah

In JavaScript, when you define an object literal, if the variable you’re using for one of the values has the same name as the key, you can just use that name, you don’t need to write

const addToDo = (todo) => {
  return {
    type: 'ADD_TO_DO',
    todo: todo,
  };
};

As another example:

function makeMeAnObject (foo, bar) {
  return {
    foo,
    bar
  };
}

Which returns:

> makeMeAnObject("hello", "world")
{ foo: "hello", bar: "world" }
1 Like

It’s ES6 syntax. When the key name is the same with the value name, you can use the shorthand and simply pass the key name.

1 Like