GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — JavaScript - Extract First Word from a String - GeeksforGeeks JavaScript - Extract First Word from a StringLast Updated : 23 Jul, 2025 Here are the different methods to extract the first word from a string Using split() Method - Most PopularThe split() method splits the string into an array of words based on spaces, and the first element of this array is the first word.
Output Hello Using substring() and indexOf() MethodsUsing 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.
Output Hello Using Regular ExpressionA regular expression can match the first sequence of non-space characters.
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.
Output Hello Using trim() and split()You can first trim the whitespace using trim(), then split() to extract the first word.
Output Hello Using Array.from() and for...of LoopThis method converts the string into an array using Array.from(), then iterates character by character to get the first word manually.
Output Hello Importance of Extracting the First Word in JavaScriptExtracting the first word from a string is useful for
Comment Explore |
| 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. |