Workshop › Text detective › Step 6 of 13
Clear the box and the detective still says "Words: 1": splitting an empty text gives one empty piece. We handle that case on its own.
```js
const text = box.value.trim()
const words = text === '' ? [] : text.split(/\s+/)
```text is the trimmed text, kept in a variable because we use it twice.condition ? a : b picks a when the condition is true, otherwise b: an empty array for an empty text.Replace the words line with the two lines.
Skills: Types and values
← The extra-space trap Count each word →