How To Get The first Character Of A String In JavaScript

Get the first character of a string in JavaScript

Javascript has many built-in methods that help us interact with strings. To get the first character of a string in JavaScript, I will use two methods, charAt() and slice(). Follow this article to understand better.

Get The First Character Of A String In JavaScript

Method 1: Use charAt() method

CharAt() the result returns a character of the string at the position specified by the parameter we passed. We can use it to get the first Character of a String in JavaScript quickly.

Syntax

charAt(index)

Parameters

  • index: This is the address number of the Character you want to retrieve.

Example:

var myString = "Get first Character";
var firstCharacter = myString.charAt(0);
console.log(firstCharacter);

Ouput:

G

In this example, I get the first Character by passing the position of the first value as 0. You can also get any word in the string by passing the argument to that Character’s position in the string.

Example:

var myString = "Get first Character";
var firstCharacter = myString.charAt(8);
console.log(firstCharacter);

Ouput:

t

In addition, we can also call the first Character as follows:

Example:

var myString = "Get first Character";
var firstCharacter = myString[0];
console.log(firstCharacter);

Output:

G

Method 2: Use slice() method

Slice() method is used to split the string, and the newly split string is a new string with a length less than the original string, which is part of the original string. You can use it to get a string’s first or any character.

Syntax

slice(start, end)

Parameters:

  • start: This is the position of the starting Character you want to split
  • end: This is the end position (do not take the end position)

If you want to get the first value of the array, pass start as 0 and end as 2:

Example:

var myString = "Get first Character";
var firstCharacter = myString.slice(0,1); 
console.log(firstCharacter);

Output:

G

If only one parameter is passed, that will be the starting position and cutting to the last Character of the original string.

Example:

var myString = "Get first Character";
var firstCharacter = myString.slice(4); 
console.log(firstCharacter);

Output:

first Character

If the parameter you pass in is one less than zero, the position you pass in will be counted starting from the end.

Example:

var myString = "Get first Character";
var firstCharacter = myString.slice(-15,-10); 
var firstCharacter2 = myString.slice(-9); 
console.log(firstCharacter);
console.log(firstCharacter2);

Output:

first
Character

Summary

There are many ways to get the first character of a string in JavaScript, but in this tutorial, I showed you two methods CharAt() and slice(). These two methods are concise and easy to implement. I hope you follow the article carefully and apply it to your program wish you success.

Maybe you are interested:

Leave a Reply

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