How to Add Two Numbers in JavaScript

Posted by

To add two numbers in JavaScript, you can use the + operator. This operator is used for both addition and concatenation (joining strings together). In the case of adding numbers, the + operator will perform arithmetic addition.

Here’s an example of how to add two numbers in JavaScript:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let num1 = 5;
let num2 = 10;
let sum = num1 + num2;
console.log(sum); // Output: 15

[/dm_code_snippet]

In this example, we have two variables num1 and num2 which hold the values 5 and 10, respectively. We use the + operator to add these two numbers together and store the result in a new variable called sum. Finally, we log the value of sum to the console using the console.log() function.

You can also use the += operator to add a number to a variable and reassign the result to the same variable. For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let num = 5;
num += 10; // num is now 15

[/dm_code_snippet]

This is equivalent to:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let num = 5;
num = num + 10; // num is now 15

[/dm_code_snippet]

You can also use the - operator to subtract one number from another. For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let num1 = 10;
let num2 = 5;
let difference = num1 - num2;
console.log(difference); // Output: 5

[/dm_code_snippet]

You can also use the * operator to multiply two numbers and the / operator to divide two numbers.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let num1 = 10;
let num2 = 5;

let product = num1 * num2;
console.log(product); // Output: 50

let quotient = num1 / num2;
console.log(quotient); // Output: 2

[/dm_code_snippet]

Here are a few additional points to consider when working with numbers in JavaScript:

  • If you want to round a number to the nearest whole number, you can use the Math.round() function. For example:

    [dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

    let num = 5.6;
    let roundedNum = Math.round(num);
    console.log(roundedNum); // Output: 6

    [/dm_code_snippet]

  • If you want to round a number up or down to the nearest whole number, you can use the Math.ceil() and Math.floor() functions, respectively. For example:

    [dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

    let num = 5.6;
    let roundedUp = Math.ceil(num);
    console.log(roundedUp); // Output: 6
    
    let roundedDown = Math.floor(num);
    console.log(roundedDown); // Output: 5

    [/dm_code_snippet]

  • If you want to find the absolute value of a number (i.e., the positive version of the number), you can use the Math.abs() function. For example:

    [dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

    let num = -5;
    let absoluteValue = Math.abs(num);
    console.log(absoluteValue); // Output: 5

    [/dm_code_snippet]

  • If you want to find the maximum or minimum of two or more numbers, you can use the Math.max() and Math.min() functions, respectively. For example:

    [dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

    let num1 = 10;
    let num2 = 5;
    let max = Math.max(num1, num2);
    console.log(max); // Output: 10
    
    let min = Math.min(num1, num2);
    console.log(min); // Output: 5

    [/dm_code_snippet]