What's with this syntax?

From the challenge here:

const removeItem = (index) => {
return {
type: ‘REMOVE_ITEM’,
index
}
}

Is index a key with a value that’s not defined? What the rules of this syntax? I feel like I’m missing something here. Maybe I’m looking at it wrong.

Hi @Rutherford

I believe you have a typo, your using backtick literals intead of quotes. you also need to remove the ( , ) you already have return statement.

that’s in the provided code and part of the “official”(on github) solution.

Ok that makes sense, so this is what the code its telling.

// your creating a function to an array  of items that will be remove( const removeItem) 
// (index) its the current element you will be removing based on the index of the item.
// and eventually return it, with an object to be able to used.
// This will be used in this function const immutableReducer()
const removeItem = (index) => {
return {
type: ‘REMOVE_ITEM’,
index
}
}

It’s shorthand, same as index: index, see here under the “Property Definitions” header:

1 Like

Imagine this:

Say you had these variables:

const name = 'Joe';
const age = 21;
const weight = 170;

Now you could do this:

const obj = {
    name: name,
    age: age,
    weight: weight
};

Redundant no?

So you can shorthand it with:

 const obj = {
    name,
    age,
    weight
};

Only works when key and its value (variable) are the spelled same.

1 Like

thanks! I had forgotten this syntax from earlier lessons.