0

Why does this work:

var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(
  word => word.length > 6
);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

but after modifying this piece, doesn't:

const result = words.filter(
      word => {
        word.length > 6
      }
    );

Notice that I want to place word.length > 6 inside accolades where I want to actually have more complex intermediate calculations (more than 1 line).

Any advice please thanks.

annepic
  • 1,147
  • 3
  • 14
  • 24

2 Answers2

0

You need to use the return keyword when your expression is surrounded by brackets:

const result = words.filter(
  word => {
    return word.length > 6
  }
);
yadejo
  • 1,910
  • 15
  • 26
0

First version:

var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
    const result = words.filter(word => word.length > 6);
    console.log(result)

Second Version

    var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
        const result = words.filter(word => {return word.length > 6});
        console.log(result)

Use first version when you have a single line expression while using second version while you have more complex condition

Isaac
  • 12,042
  • 16
  • 52
  • 116