JavaScript Code


Add Two Numbers – JavaScript Program


Add Two Numbers Program

In this tutorial, you are given two numbers. Write a JavaScript program with a function that takes two numbers as arguments, add the two numbers, and returns the result.

Solution

To find the sum of two numbers in JavaScript, we can use arithmetic addition operator. Addition operator takes two numbers as operands, and returns their sum.

Program

In the following program, we take two numbers in a, b; find their sum, and print the result to console output.

function addTwoNumbers(num1, num2) {
    return num1 + num2;
}

var a = 5;
var b = 4;
const result = addTwoNumbers(a, b);
console.log(result);