How to get first character of a string in JavaScript

How to get first character of a string in JavaScript

In this post we will give you information about How to get first character of a string in JavaScript. Hear we will give you detail about How to get first character of a string in JavaScriptAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to get the first character of a string in JavaScript.

Getting the first character

To get the first character of a string, we can use the charAt() method by passing 0 as an argument in JavaScript.

The charAt() method accepts the character index as an argument and return its value in the string.

Strings are the squence of characters, so the first character index of a string is 0.

const str = "helicopter";const firstCharacter = str.charAt(0);console.log(firstCharacter); // "h"

Similarly, we can use square brackets [] syntax to get the first character of a string.

const str = "helicopter";const firstCharacter = str[0];console.log(firstCharacter); // "h"

Hope this code and post will helped you for implement How to get first character of a string in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US