Como obter o primeiro personagem de uma string em JavaScript
let str = 'John Wick'
let firstChar = str.charAt(0)
console.log(firstChar); // "J"
Busy Buffalo
let str = 'John Wick'
let firstChar = str.charAt(0)
console.log(firstChar); // "J"
let myStr = "Hello World"
let firstWord = myStr.split(" ")[0]
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
let string = "Hello World"
let firstWord = typeof string.split(" ")[0] !== 'undefined' ? string.split(" ")[0] : null;
let sentence = "The big brown fox"
let word = sentence.split(" ")[0]
console.log(word) // The