Hi Friends 👋,
Welcome To Infinitbility! ❤️
To concat string in react js, you can use +
plus operator or ES6 string interpolation both will help for your string concatenation.
In the following example, we will take the sample string variable, and do concatenation using both ways ( plus operator and string interpolation ).
1let text = "infinitbility.com";23// using plus operator4let str1 = "One of the best site is " + text;5console.log(str1) // "One of the best site is infinitbility.com"67// string interpolation8let str2 = `One of the best site is ${text}`;9console.log(str2) // "One of the best site is infinitbility.com"
Today, I’m going to show you How do i concat string in react js, as mentioned above here, I’m going to use the plus operator and string interpolation to concatenate two strings.
Let’s start today’s tutorial how do you concat string and variable in react js?
In this example, we will do
- take an example of a string state
- use the plus operator and string interpolation
- print the concatenation string on the page screen
1import React, { useState, useEffect } from "react";2export default function App() {3 const [sampleString, setSampleString] = useState("infinitbility");45 useEffect(() => {6 // using plus operator7 let str1 = "One of the best site is " + sampleString;8 console.log(str1)9 }, []);1011 return (12 <div className="App">13 <h1>{`One of the best site is ${sampleString}`}</h1>14 </div>15 );16}
In the above react js example, we have taken examples of both ways ( plus operator and string interpolation ) and printed output in the console and page screen.
I hope it helps you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.