Javascript How to get first three characters of a string
slice(begin, end)
works on strings, as well as arrays. It returns a string representing the substring of the original string, from begin
to end
(end
not included) where begin
and end
represent the index of characters in that string.
const string = "0123456789";
console.log(string.slice(0, 2)); // "01"
console.log(string.slice(0, 8)); // "01234567"
console.log(string.slice(3, 7)); // "3456"
See also:
- What is the difference between String.slice and String.substring?
var str = '012123';
var strFirstThree = str.substring(0,3);
console.log(str); //shows '012123'
console.log(strFirstThree); // shows '012'
Now you have access to both.