Skip to content

How to cut a string into words in JavaScript

Use the split() method of a string instance. It accepts an argument we can use to cut the string when we have a space:

const text = "Hello World! Hey, hello!"
text.split(" ")

The result is an array. In this case, an array with 4 items:

[ 'Hello', 'World!', 'Hey,', 'hello!' ]

→ Get my JavaScript Beginner's Handbook

I wrote 21 books to help you become a better developer:

  • HTML Handbook
  • Next.js Pages Router Handbook
  • Alpine.js Handbook
  • HTMX Handbook
  • TypeScript Handbook
  • React Handbook
  • SQL Handbook
  • Git Cheat Sheet
  • Laravel Handbook
  • Express Handbook
  • Swift Handbook
  • Go Handbook
  • PHP Handbook
  • Python Handbook
  • Linux Commands Handbook
  • C Handbook
  • JavaScript Handbook
  • Svelte Handbook
  • CSS Handbook
  • Node.js Handbook
  • Vue Handbook
...download them all now!

Related posts that talk about js: