get first word from a string and convert to lowercase in javascript

You can use the substr method to get the first word from a string and then use the toLowerCase method to convert it to lowercase.

Here's an example code snippet:

index.tsx
const string = "Hello World";
const firstWord = string.substr(0, string.indexOf(" ")).toLowerCase();
console.log(firstWord); // Output: "hello"
144 chars
4 lines

In the code above, we first declare a variable string which contains the input string. We then use substr to extract the first word from the string, starting from index 0 and ending at the position of the first space character in the string (using indexOf method).

Finally, we apply toLowerCase method to convert the first word to lowercase and assign the result to the firstWord variable. The output is then logged to the console.

gistlibby LogSnag