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.