How to get first word of a string using JavaScript.

 

You just need to split string with a blank space using String.split() method

 

let string = 'some text';
let firstWord = string.split(' ')[0];

console.log( firstWord ); // some

 

Another way to get first letter of string 

let string = 'some text';
let firstWord = string.split(' ').shift();;

console.log( firstWord ); // some

Tags:

Share:

Related posts