How to Get First Character of String in React JS?

Oct 27, 2022 . Admin

Hello Friends,

In this tutorial, you will learn how to get the first character of a string in to react js. This tutorial will give you a simple example of get the first character of a string in react js. I’m going to show you about react js to get the first character of the string. This post will give you a simple example of react js get the first character of string example. follow the bellow step to get first character of the string using react js.

in this example get first character of string in react js. I will step by step get string first character using str.charAt().

So, let's start following example:

Step 1: Create React JS App

In this step, open your terminal and execute the following command on your terminal to create a new react app:

npx create-react-app reactjs-my-example
Step 2: Create index.js file src/index.js
import React from 'react';
import ReactDOM from 'react-dom/client';

function App(){
    
    const str = "Mywebtuts.com";

    const firstCharacter = str.charAt(0);
    
    return (
        <div>
            <h1>{firstCharacter}</h1>
        </div>
    );
}
  
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
Run React JS App:
npm start

Output:

M

I hope it can help you...

#React JS