Snippets

Multiply Two Numbers with JavaScript

Posted by I. B. Gd Pramana A. Putra, 12 Mar 22, last updated 17 Jul 22

This is a JavaScript code example or code snippet to multiply two numbers. This code contains some examples of how to create a simple program that multiplies two numbers and return the result.

JavaScript Code Example: Multiply Two Numbers

The following is an example of a JavaScript code to multiply two numbers. The user will be asked to input two numeric values, then store the values of those two numbers on their own variable, then multiply the two numbers and display the result.

In this two numbers multiplication with JavaScript, we will use the Arithmetic Operators in JavaScript. The arithmetic operator for multiplication in JavaScript is the * symbol.

const firstVal = parseInt(prompt('Please input the value for firstVal variable: '));
const secVal = parseInt(prompt('Please input the value for secVal variable: '));

const result = firstVal * secVal;

console.log(`${firstVal} * ${secVal} = ${result}`);

Output

Let’s run/execute the JavaScript program code above, and enter the value for each of the numbers requested, for example, the number 8 and number 4, then the results are as follows:

Please input the value for firstVal variable: 8
Please input the value for secondVal variable: 4
8 * 4 = 32

Hopefully, this JavaScript code snippet program to multiply two numbers is useful.

I love sharing code snippets as most of the time, a quick code example is what we're looking for instead of long-written articles. If you think my code snippets are helpful and save you a lot of time, please consider buying me a cup of coffee :)

Support me via · paypal · buymeacoffee · ko-fi · trakteer
Contributed Snippets
Answer & Responses
    No comments yet

Wanna write a response?

You have to login before write a comment to this post.