Javascript How to get first three characters of a string

Javascript How to get first three characters of a string

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.

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:

Javascript How to get first three characters of a string

Leave a Reply

Your email address will not be published. Required fields are marked *