Home » 5 ways to Get First Character From a String using JavaScript

5 ways to Get First Character From a String using JavaScript

In this tutorial let’s learn about How to Get First Character From a String using JavaScript.

Strings are used in JavaScript to represent and operate with a sequence of characters. A string can represent both an object and a primitive data type. JavaScript automatically converts basic strings to String objects, allowing primitive strings to use String methods and access properties.

Get First Character From a String using charAt() in JavaScript

The character at a particular index (position) in a string is returned by the charAt() method. The first character has an index of 0, the second one has an index of 1, and so on. The last character’s index is string length – 1.

This method returns the single UTF-16 code unit found at the given index. The original string is not mutated or modified by this procedure.

Syntax:

string.charAt(index)

Any character found at the index is added into the new string. If no index is specified, the value 0 will be used as the default.

Code Example :

const str = "Similar Geeks";
const firstCharacter = str.charAt(0);

console.log(firstCharacter);

Output :

S

Get First Character From a String using slice() in JavaScript

The slice() method is an in-built JavaScript method. This method splits the string in two. This cut is performed by taking two inputs, the beginning and end indices.

And it will return the part of the string between the indexes based on that. It will return to the last character if only the beginning index is provided.

Syntax:

string.slice(starting_index, ending_index)

Code Example :

const str = "Similar Geeks";
const firstCharacter = str.slice(0, 1);

console.log(firstCharacter);

Output :

S

Get First Character From a String using substr() in JavaScript

The substr() method is a built-in method in JavaScript. This method splits the string in two. This split is performed by taking two inputs: the start index and the total amount of characters to be cut.

Based on that, it will return the part of the string between the index and the entire number. If only the start index is given, it will return the end of string.

Syntax:

string.substr(starting_index , length)

Code Example :

const str = "Similar Geeks";
const firstCharacter = str.substr(0, 1);

console.log(firstCharacter);

Output :

S

Get First Character From a String using substring() in JavaScript

The substring() method is a JavaScript built-in method. This method splits the string in two. This slice is performed by taking two inputs, the start and end indexes.

And it will return the section of the string between the indexes based on that. If only the start index is given, it will return the end of the string.

Syntax:

string.substring(start , end )

Code Example :

const str = "Similar Geeks";
const firstCharacter = str.substring(0, 1);

console.log(firstCharacter);

Output :

S

Get First Character From a String using indexing in JavaScript

Similar to charAt() Method we can also use square brackets [] to get the character present at given index value.

The below example uses [] to get the first character of the string, which is at index 0.

Code Example :

const str = "Similar Geeks";
const firstCharacter = str[0];

console.log(firstCharacter);

Output :

S

Conclusion

In this tutorial we learnt different ways to extract the first character of the string using JavaScript Functions.

To extract or get the first character of a string in JavaScript you can use charAt(), slice(), substr(), substring() or simple indexing using square brackets [].

Similar Posts:

String Interpolation in JavaScript
How to Check if a String is Empty in JavaScript