How To Concatenate Strings And Variables In React

Concatenate strings and variables in react

It’s very easy to concatenate strings and variables in React. Let me show you how right now. Please read the entire article to understand what I want to convey.

Concatenate strings and variables in React

To concatenate strings and variables, we use template literal, also known as template string. This is a syntax to declare String in JavaScript introduced in ES6. Template literal allows us to use variables, expressions, or functions inside a String without doing String addition. 

Unlike the regular String, in template literal, we use the back-ticks instead of apostrophes or double quotes. Variables, expressions, and functions are placed in ‘${}‘. You can put any variables, expressions, or functions inside ‘${}‘.
The program shows how to use it:

Check out the following example for a better understanding:

In Demo.css

  text-align: center;
  color: darkslateblue;

  color: red;

  color: green;

In Demo.js

import './Demo.css';

export default function Demo() {
    const Student = {
      name: 'Jason',
      id: '20187229',
      gender: 'Male',
      mathScore: 8.8,
      englishScore: 9.0,
      literatureScore: 7.0

    function finalGrade (s1,s2,s3) {
      const result = (s1 + s2 + s3)/3
      return result.toFixed(2)

    const infor = `INFORMATION OF ${} - ID: ${}, GENDER: ${Student.gender}`
    return (
     <div className='container'>
      <h3>{`Your final grade is: ${finalGrade(Student.mathScore,Student.englishScore,Student.literatureScore)}`}</h3>
      { finalGrade(Student.mathScore,Student.englishScore,Student.literatureScore) > 6.5 ? <h3 className='pass'>Eligibility to graduate</h3> : <h3 className='caution'>Not eligible to graduate</h3> }


The above example uses an object named ‘Student‘. We get the properties of this object and put them in the ‘${}‘ of a string. As a result, the values ​​are rendered correctly in the HTML template.

The wrapping of curly braces marks the beginning and the end of an expression. In this example, I have used a conditional expression in curly braces. If the student’s overall score is greater than 6.5, then ‘Eligibility to graduate‘ will be rendered. Otherwise, ‘Not eligible to graduate‘ will be rendered.

An important thing to note is that when using template literal, we do not use apostrophes or double quotes but the back-ticks. Using the wrong symbol is a common mistake when working with template literal.

const str1 = "Congratulations!"   //using double quotes 
const str2 = `Congratulations!`  // using back-ticks




Of course, the result will be the same. However, these characters are easily confused with each other. Please pay attention when using them to avoid unexpected errors.


Through the above simple example, I hope you understand how we can concatenate strings and variables in React. Thank you for reading to the end.

Maybe you are interested:

Leave a Reply

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