Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To get first two character of string in javascript, use substring() method to extracts first two character from string. You have to only pass starting and ending postion of string so for first two character pass 0, 2 as parameter it will return first two character of string.

Let’s see short example to use substring() to get first two character from string.

text.substring(0, 2);

Today, I’m going to show you How do I get first two character of string in javascript, as above mentioned, I’m going to use the above-mentioned substring() method.

Let’s start today’s tutorial how do you get first two character of string in javascript?

Javascript get first two character of string example using substring()

Here, we will take string variable with some data and use substring() method to get first two char of string.

let str = "Infinitbility";

let char = str.substring(0, 2);
console.log(char);

// Output
// 'In'

Output

Javascript get first two character of string example
Javascript get first two character of string example

I hope it helps you, All the best đź‘Ť.