JavaScript Get First Character Of String

JavaScript Get First Character Of String | We occasionally need to retrieve the first character of a JavaScript string. We’ll examine how to obtain the first character of a JavaScript expression in this tutorial using different methods. Following are the different ways in JavaScript to get the first character of a string:-

  • Using charAt() method
  • Using [ ]
  • Using substring() method
  • Using slice() method

JavaScript Get First Character Of String Using charAt() Method

By giving 0 as a parameter to JavaScript’s charAt() method, we can retrieve the initial character of a string. Since strings are collections of characters, their first character index is always 0.

const str = "Know Program";
const first = str.charAt(0);
console.log("First character = " , first)

Output:-

First character = K

Javascript How To Get First Character Of A String Using [ ]

Alternatively, we may obtain the first character in a string using the square brackets [ ] syntax.

const str = "Know Program";
const first = str[0];
console.log("First character = " , first)

Output:-

First character = K

How To Get First Character Of A String In JavaScript Using substring() method

By providing the start and end indexes, the substring method enables us to extract a substring from a string. To get the first character of a string we have to pass 0 and 1 as the first and second parameters to the substring() method.

const first = 'Hello world'.substring(0, 1)
console.log("The first character of the string is = " , first)

Output:-

The first character of the string is = H

Get First Char Of String JS Using slice() Method

The slice() method allows us to obtain a substring containing the start and end indexes, just like the substring function does. The character at the starting index is included, just like in a substring. The character in the final index, however, is not.

For comparison, we could write:

const first = 'Know Program'.slice(0, 1)
console.log("The first character of the string is = " , first)

Output:-

The first character of the string is = K

The initial character of a string can be obtained in JavaScript in a number of different ways. Square brackets or string approaches can be used to accomplish this. 

Check First Two Characters Of String Javascript

To check the first two characters of the string, first, we have to see if that string length is greater than or equal to 2 or not. If the given string length is less than 2 then we can’t get the first two characters of the string.

const string = 'Know Program'
let firstChar = null
let secondChar = null
if(string.length >= 2) {
    firstChar = string.charAt(0);
    secondChar = string.charAt(1);
}
console.log("First character = " , firstChar)
console.log("Second character = " , secondChar)

Output:-

First character = K
Second character = n

Program to Get First 10 Characters Of String Javascript

To get the first 10 characters of a string in JavaScript, we have to check the length of the string. If the length of the given string is less than 10 then we will print “length of the string is less than 10”. Else we will use for loop to iterate the string and fetch the character of the given string.

const string = 'Know Program';
if(string.length >= 10) {
    for (let i = 0; i < 10; i++) {
        const element = string.charAt(i);
        console.log(element);
    }
} else {
    console.log("Length of the string is less than 10");
}

Output:-

K
n
o
w

P
r
o
g
r

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.