Signup/Sign In
PUBLISHED ON: DECEMBER 26, 2022

JavaScript Program For Addition of two numbers

Welcome to this tutorial on how to create a JavaScript program for adding two numbers! In this tutorial, we will go over the steps needed to create a simple program that takes two numbers as input and returns their sum. This tutorial is aimed at beginners who are new to programming and want to get a basic understanding of how to write and run a JavaScript program.

JavaScript is a popular programming language that is used to build websites and web applications. It is known for its versatility and flexibility, and it is used by developers all over the world to create interactive and dynamic web experiences. In this tutorial, we will be using JavaScript to create a program that adds two numbers together, but the skills you learn here can be applied to many other types of programs and projects.

So, without further ado, let's get started!

Approach

  • Take 2 input variables.
  • We can add two numbers in JavaScript by using the addition operator +.
  • The Addition operator takes two operands and returns their sum.

Program For Addition of two numbers

// JavaScript Program to Add two Numbers 
const n1 = parseInt(prompt('Enter the first number '));
const n2 = parseInt(prompt('Enter the second number '));

//adding the two numbers
const sum = num1 + num2;

// displaying the sum
console.log(`The sum of ${n1} and ${n2} is ${sum}`);

Input

Enter the first number 12

Enter the second number 28


The Sum of 12 and 28 is 40

Conclusion

In conclusion, we have successfully learned how to create a simple JavaScript program for adding two numbers together. We started by discussing the basics of JavaScript and its role in web development, and then we walked through the process of writing and running a JavaScript program. We learned how to take user input, perform a calculation, and display the result to the user.

While this tutorial focused on a basic example, the skills you have learned here can be applied to many other types of programs and projects. As you continue to learn and grow as a programmer, you will be able to build on these foundational concepts and create more complex and powerful programs. Keep practicing, experimenting, and learning, and you will soon be on your way to becoming a proficient JavaScript developer.

Take the first step towards becoming a JavaScript pro by signing up for our Interactive Course now!



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.