how to get first 2 character of string in javascript like this "Hello world " => get this "wo"? another example "Osama Mohamed" => M.
you can use string.split(' ')
, this function will split you string into an array, for example:
let s = "Hello World";
console.log(s.split(" ")) // will log ["Hello", "World"]
then you can get the second word using array[index]
when index the index of your desired word in said array.
now using string charAt
we can get the first letter of the word.
now to put everything together:
let s = "Hello World"
let s_splited = s.split(" ") // ["Hello", "World"]
let second_word = s_splited[1]
console.log(second_word.charAt(0))
string = "Hello world";
split_string = string.split(" ");
second_word = split_string[1]
first_char = second_word[0]
console.log(first_char)
OR
string = "Hello world";
console.log(string.split(" ")[1][0])