xxxxxxxxxx
let sentence = "The big brown fox"
let word = sentence.split(" ")[0]
console.log(word) // The
xxxxxxxxxx
let str = 'John Wick'
let firstChar = str.charAt(0)
console.log(firstChar); // "J"
xxxxxxxxxx
const str = "What day of the week is it?";
// 1) the match() method:
str.match(/^\w+\s/)[0];// => What
// 2) the split() method:
str.split(' ')[0]; // => What
// 3) the slice() method:
str.slice(0, str.indexOf(' ')); // => What
xxxxxxxxxx
let string = "Hello World"
let firstWord = typeof string.split(" ")[0] !== 'undefined' ? string.split(" ")[0] : null;
xxxxxxxxxx
var str = "Java Script Object Notation";
var matches = str.match(/\b(\w)/g); // ['J','S','O','N']
var acronym = matches.join(''); // JSON
console.log(acronym)
xxxxxxxxxx
const string = 'Hello';
const firstCharacter = string.charAt(0);
console.log(firstCharacter);
xxxxxxxxxx
const str = "Hello world";
const firstChar = str.charAt(0); // Retrieves the first character of the string
console.log(firstChar); // Outputs 'H'