GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Introduction3 min read
  • Variables5 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript - Extract First Word from a String - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript - Extract First Word from a String

Last Updated : 23 Jul, 2025

Here are the different methods to extract the first word from a string

Using split() Method - Most Popular

The split() method splits the string into an array of words based on spaces, and the first element of this array is the first word.

JavaScript
const s1 = "Hello World, welcome to GeeksforGeeks";

const s2 = s1.split(" ")[0];
console.log(s2);

Output
Hello

Using substring() and indexOf() Methods

Using indexOf() we can find the first space, then extracts everything before the space using substring(). This approach is useful if you need only the first word without further splitting.

JavaScript
const s1 = "Hello World, welcome to GeeksforGeeks";

// Extract the first word
const s2 = s1.indexOf(" ");
const s3 = s2 === -1 ? s1 : s1.substring(0, s2);
console.log(s3);

Output
Hello

Using Regular Expression

A regular expression can match the first sequence of non-space characters.

JavaScript
const s1 = "Hello World, welcome to GeeksforGeeks";

// Extract the first word using regex
const s2 = s1.match(/\S+/)[0];
console.log(s2);

Output
Hello

Using slice() and indexOf()

Similar to substring(), slice() can be used to extract characters up to the first space. This approach can also handle cases where there's no space in the string.

JavaScript
const s1 = "Hello World, welcome to GeeksforGeeks";

// Extract the first word
const s2 = s1.slice(0, s1.indexOf(" ") !== -1 ? s1.indexOf(" ") : s1.length);
console.log(s2);

Output
Hello

Using trim() and split()

You can first trim the whitespace using trim(), then split() to extract the first word.

JavaScript
const s1 = "Hello World, welcome to GeeksforGeeks";

const s2 = s1.trim().split(" ")[0];
console.log(s2);

Output
Hello

Using Array.from() and for...of Loop

This method converts the string into an array using Array.from(), then iterates character by character to get the first word manually.

JavaScript
const s1 = "Hello World, welcome to GeeksforGeeks";

// Extract first word manually
let s2 = '';
for (const char of Array.from(s1)) {
    if (char === ' ') break;
    s2 += char;
}
console.log(s2);

Output
Hello

Importance of Extracting the First Word in JavaScript

Extracting the first word from a string is useful for

  • Data Parsing: Extracting titles or key information from strings.
  • Improving User Interactions: Displaying simplified information for readability.
  • Text Processing: Assisting in tasks like summarizing or analyzing data.
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.