Extracting Characters from Strings
Description :
In Javascript, there are different ways of extracting a characters from a string.
Some of these are :
charAt(position)
The charAt(position) returns the character at a specific index. The first character starts at index 0.
Syntax :
let character = original_string.charAt(position_number_zero_indexed);
Examples :
let str = "HELLO WORLD";
let character = str.charAt(0);
character = ??
let str = "HELLO";
let character = str.charAt(4);
character = ??
charCodeAt(position)
The charCodeAt(position) method returns the unicode of the character present at index = position in the string
Syntax :
let character = original_string.charCodeAt(position_number_zero_indexed);
Examples :
let str = "HELLO WORLD";
let character = str.charCodeAt(0);
character = ??
Property Access []
You can also directly get the character by saying string_name[position]
Syntax :
let character = original_string[position_number_zero_indexed];
let str = "HELLO WORLD";
let character = str[0];
character = ??