Search code examples
javascriptreduceternary

JavaScript reduce ternary operator


This is silly, but I'm having hard time understanding how these two statements outputs different results.

'theyyyyy wheels on q bus'.split(' ').reduce((shortest, w) => {
   return  w.length < shortest.length ? shortest = w : shortest;
 }) // 'q'

'theyyyyy wheels on q bus'.split(' ').reduce((shortest, w) => {
  return shortest.length > w.lenght ? shortest = w : shortest;
}) // 'theyyyyy'

Solution

  • In the second case, you have w.lenght which is undefined. Change to w.length.