Open In App

JavaScript Program for Multiplication of Two Numbers

Last Updated : 20 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how we can multiply two numbers using JavaScript. Multiplying the two numbers in JavaScript involves performing arithmetic addition on numeric values, resulting in their sum.

JavaScript supports numeric data types. In JavaScript, both floating-point and integer numbers fall under the same “Number” type. Because JavaScript takes care of it for you, you don’t need to explicitly declare whether a number is an integer or a floating-point number. In order to avoid having to declare them separately, we can use a variable and assign it to any value.

Examples:

Let's take two numbers 
a = 4 , b = 10
 
Here a is multiplicand and  b is multiplier 
a * b = 4 * 10 = 40

These are the following ways by which we can multiply two numbers:

Using “*” Operator

In this approach we multiply two numbers in JavaScript involves using the * operator to perform arithmetic multiplication on numeric variables, resulting in their result.

Syntax:

a * b ;

Example:

Javascript




// Creating first variable
let a = 10;
  
// Creating second variable
let b = 10;
let result = a * b;
  
// Printing the result
// in console
console.log("Result :", result);


Output

Result : 100

Using Functions

Functions are the building blocks of some specific code that carry out specific tasks. Using the function, we can multiply two numbers by passing parameters, and the function will then return the result of the multiplication.

Syntax:

function fun_name (param1 , param2) {
   return param1 * param2 ;
}
OR 
function fun_name (param1 , param2){
    return param2 * param1 ;
}
~ Although params are optional, they are necessary if you need to perform an operation.

Example: This example describes the multiplication of 2 numbers using Function.

Javascript




// First variable
let a = 10;
  
// Second variable
let b = 20;
  
// Funciton for multiplication of two numbers
function multiply(a, b) {
    return a * b;
}
  
// For printing the result
console.log("After multiplication : " + multiply(a, b));


Output

After multiplication : 200

Using Arrow function

In order to multiply two numbers, we create an arrow function, passing the values of the two numbers as parameters and returning the result of multiplication. We obtain the multiplied value by calling the arrow function and providing values for the parameter.

Syntax:

let var_name = (param) => {
      // Your code
}

Example: This example describes the multiplication of 2 numbers using Arrow function.

Javascript




// First variable
let a = 10;
  
// Second variable
let b = 20;
  
// Arrow funciton for multiplication of two numbers
let result = (a, b) => a * b;
  
// For printing the result in console
console.log("After multiplication : " + result(a, b));


Output

After multiplication : 200

Using Multiplication Assignment Operator

When multiplying a variable by a specific amount and returning the result to the variable, the multiplication assignment operator *= is a useful shorthand. In JavaScript, it is a compound assignment operator.

Example:

P = 10 
P *= 2 is equal to P = P*2 
So, now P will be 20.

Syntax:

varibale_name  *=  value/anotherVariable_name;

Example: This example is describing how we can multiply two number using multiplication assignment operator.

Javascript




// Creating a variable having value 20
let a = 20;
  
// Using multiplication assignment operator 
// Using value for multiplication
a *= 10;
  
// Printing the result
console.log(a); // 10
  
// Using another variable for mulitiplication
let b = 2;
a *= b;
  
// Printing the result
console.log(a); // 10


Output

200
400

Using for Loop

We can multiply two numbers by using for loop. A function must first be initialized before multiplication calculations can be made using a for loop.

Syntax:

for (initialization ; condition ; iteration) {
    // Code to be executed in each iteration
}

Example: This examples describes how we can multiply two numbers using for loop

Javascript




// Function for multiplication
function multiply(a, b) {
    let result = 0;
    for (let i = 1; i <= b; i++) {
        result += a;
    }
    return result;
}
  
// Calling function and storing the returned value
let result = multiply(5, 10); // 50
  
// Display the result
console.log(result);


Output

50


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads