Navigate back to the homepage

how to add two numbers in react js?

Infinitbility
React
July 4th, 2022 · 1 min read
how to add two numbers in react js?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To add two numbers in react js, use the + addition operator it will add if your value datatype is a number else if your value datatype is string first convert it using parseInt() and then perform addition operation.

In the following example, we will take the sample numbers, and strings and perform add operation using the + addition operator.

Adding Two numbers example

1let num1 = 30;
2let num2 = 34;
3
4num1 + num2; // 64

Adding Two string numbers example

1let num1 = "30";
2let num2 = "34";
3
4parseInt(num1) + parseInt(num2); // 64

Today, I’m going to show you How do i add two numbers in react js, as mentioned above here, I’m going to use the + addition operator.

Let’s start today’s tutorial how do you add two numbers in react js?

In this example, we will do

  1. take an example of a number and string number state
  2. adding two numbers example
  3. adding two string numbers example
  4. print the output on the page screen
1import React, { useState } from "react";
2export default function App() {
3 const [firstNum, setFirstNum] = useState(24);
4 const [secondNum, setSecondNum] = useState(24);
5
6 const [firstStr, setFirstStr] = useState("24");
7 const [secondStr, setSecondStr] = useState("24");
8
9 return (
10 <div className="App">
11 <h1>{`adding two numbers example`}</h1>
12 <p>{firstNum + secondNum}</p>
13
14 <h1>{`adding two string numbers example`}</h1>
15 <p>{parseInt(firstStr) + parseInt(secondStr)}</p>
16 </div>
17 );
18}

In the above react js example, we have taken the sample numbers and string numbers state, and perform adding two numbers example, and adding two string numbers example.

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.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to replace string in react js?

how to replace string in react js?

An example of replace string in react js with a step-by-step example.

July 4th, 2022 · 1 min read
how to concat string in react js?

how to concat string in react js?

An example of concat string in react js with a step-by-step example.

July 3rd, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]