Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JavaScript»JavaScript Program to Add Two Numbers

    JavaScript Program to Add Two Numbers

    RahulBy RahulJuly 6, 20212 Mins ReadUpdated:June 9, 2022

    Write a JavaScript program to calculate the sum of two integers and display the results. In this tutorial, we will show you two examples of adding two numbers.

    JavaScript Program to Add Two Numbers

    In this JavaScript program, we will calculate the sum of two numbers and print the results on the console. Here we will use the common + sign to add numbers.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    // define variable with integers
    var num1 = 3;
    var num2 = 2;
     
    // add two numbers
    var sum = num1 + num2;
     
    // Show the results
    console.log('The sum is: ' + sum);

    Run the above example:

    Calculate Sum of Two Integers in JavaScript
    Calculate Sum of Two Integers in JavaScript

    Here we have defined two variables with static integer values. Then calculate the sum of both values and store them in a third variable. Finally, the result is displayed on the console.

    Another Example with User Input

    Let’s consider another example to calculate the sum of values based on user input. First, have a look at the below program.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // Take user input
    var x = window.prompt("Enter first number: ");
    var y = window.prompt("Enter second number: ");
     
    // Convert string to integer
    var num1 = parseInt(x);
    console.log('First input numer: ' + num1)
     
    var num2 = parseInt(y);
    console.log('Second input numer: ' + num2)
     
    // Calcualte the Sum
    var sum = num1 + num2;
     
    // Show the results
    console.log('The sum of ' + num1 + ' + ' + num2 + ' is: ' + sum);

    See the result of the above example:

    Calculate Sum of Two Numbers in JavaScript
    Calculate sum of two numbers in JavaScript with user input

    This program takes input from user. Here the window.prompt() function create a popuop box in browser to accept input. This input is in string format. So next, we use parseint() function to convert a string value to a integer.

    Finally, calculate the sum of both integers and print the results. This time the results will be shown in a popup box.

    calculation javascript js
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Use Environment Variables in Node.js
    Next Article Chattr Command in Linux with Examples

    Related Posts

    What is difference between var, let and const in JavaScript?

    3 Mins Read

    How to Replace String in JavaScript

    Updated:June 13, 20222 Mins Read

    How to Install ReactJS on Ubuntu 20.04

    3 Mins Read

    Remove First Character from String in JavaScript

    1 Min Read

    10 Useful JavaScript Console Methods

    Updated:June 9, 20223 Mins Read

    JavaScript Program to Remove Duplicate Array Elements

    Updated:July 20, 20211 Min Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How To Install Docker on Ubuntu 22.04
    • How to Install Bower on Ubuntu 22.04 & 20.04
    • How to run “npm start” through Docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.