JavaScript · Beginner · 8 min
Calculate a total
Use numeric values and arithmetic to calculate a shopping cost.
Work with numbers
Numbers do not need quotes. Use + to add, - to subtract, * to multiply, and / to divide. Parentheses can group calculations.
console.log(6 + 4);
console.log(3 * 5);
console.log(12 / 3);Output
10 15 4
Convert numeric text
Number() converts a numeric string into a number. Adding two strings joins their text, so convert numeric input before calculating.
console.log("5" + "2");
console.log(Number("5") + Number("2"));Output
52 7
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Calculate the total
Set total to the cost of 4 tickets priced at 7 each, using price and quantity. Display total.
Multiply price by quantity with *.
Put it into practice
- Predict the output of each example before running it.
- Complete the coding task, then try the practice questions.