How to Get First Character of String in JavaScript

To get the first character of a string in JavaScript, use the charAt() function. The charAt() is a built-in JavaScript method that returns the character at a specified index (position) in a string.

Syntax

charAt(index)

Parameters

index –  It takes one parameter called index, between 0 and string.length-1. If we pass an index that is not valid, then it will return the first character from the string. The default value of this method is 0.

Return value

string – It will return a string from the UTF-16 code unit. If the passed index is greater than the string.length-1, it will return an empty string.

Example

const string = "Javascript is beautiful";
console.log(string.charAt(0)); // first character
console.log(string.charAt(14));

Output

J
b

We get the first character using the charAt() method above. The charAt() method copies the “0” index value and returns that.

Using JavaScript slice() method

The String slice() is a built-in JavaScript method that extracts a part of a string. It returns the extracted part in a new string and does not change the original string.

Syntax

string.slice(startIndex, endIndex);

Parameters

startIt is the required parameter which is the start position.

end: It is the optional parameter which is the end position.

Example

const string = "Javascript is beautiful";
console.log(string.slice(0, 1)); // first character
console.log(string.slice(14, 15));

Output

J
b

You can see that we extracted the first character using a string.slice() method.

Using substring() method

The String.substring() is a built-in JavaScript method that extracts characters, between two indices (positions), from a string and returns the substring.

Syntax

string.substring(start, end)

Parameters

start: It is a required parameter, the start position.

end: It is the optional parameter and the end position.

Example

const string = "Javascript is beautiful";
console.log(string.substring(0, 1)); // first character
console.log(string.substring(14, 15));

Output

J
b

Using the string.substring() method, we extracted the first character from a string.

Using substr() method

The String.substr() is a built-in JavaScript method used to extract a substring from a string, given a start position and a length.

Syntax

string.substr(startIndex,length);

Parameters

The startIndex is a required parameter that is the start position of the string.

The length is an optional argument about the number of characters to extract.

Example

const string = "Javascript is beautiful";
console.log(string.substr(0,1)); // first character
console.log(string.substr(14,1));

Output

J
b

Using [] notation

const string = "Javascript is beautiful";
console.log(string[0]); // first character
console.log(string[14]);

Output

J
b

We are using “[]” to access the string elements directly via its index, and it returns the element based on the index.

Using spread operator

The spread operator creates an array of strings and stores them in a variable, and then we access them as array elements.

const string = "Javascript is beautiful";
const arr = [...string];

console.log(arr[0]);
console.log(arr[14]);

Output

J
b

We converted the string into an array using the […] spread operator and accessed array elements using the box bracket.

Final words

The best way to get the first character from a string in JavaScript is to use the charAt() function. It is the most optimal way to do that. You can use other methods based on your requirements.

That’s it for this tutorial.

Related posts

How to Get Character from String in Javascript

How to Convert String to Char Code in Javascript

How to Convert String to Object in JavaScript

Leave a Comment