How to use split() Method In Javascript
In this approach, we are using the split() method to extract the first word from the input string. The split() method in JS is used to split the string into the array of substrings and from this substring, we extract the first word which is stored at the 0th index.
Syntax
string.split(separator, limit);
Example: In this example, we will be extracting the first word from a string using the split() Method.
let str = "Geeks For Geeks";
let res = str.split(' ')[0];
console.log(res);
Output
Geeks
JavaScript Program to Extract First Word from a String
In this article, we have to extract the first word from the input string in JavaScript language. Below is an example for better understanding.
Examples:
Input: Geeks for Geeks Output: Geeks Input: I Love India Output: I
Contact Us