Write a program in JavaScript to add the values of any two variables.




Write a program in JavaScript to add the values of any two variables.

 // Function to add two variables

function addVariables(variable1, variable2) {

    // Check if both arguments are numbers

    if (typeof variable1 === 'number' && typeof variable2 === 'number') {

        // Add the values of the variables

        return variable1 + variable2;

    } else {

        // If either or both arguments are not numbers, return an error message

        return "Both variables should be numbers.";

    }

}


// Example usage:

let a = 5;

let b = 10;

let sum = addVariables(a, b);

console.log("Sum of", a, "and", b, "is:", sum); // Output: Sum of 5 and 10 is: 15


Post a Comment

0 Comments