EasyCode365EasyCode365

Numbers and Basic Math

In this lesson, we will learn how to use numbers and perform simple math operations like addition, subtraction, multiplication, and division. Whether you are calculating a total price or keeping track of a game score, numbers are an essential part of programming.

What numbers are in JavaScript

In JavaScript, a number is a basic data type used to represent numeric values. You can use whole numbers (like 10 or -5) and numbers with decimals (like 3.14).

When you write a number in your code, you just type the digits directly without any quote marks around them. If you put a number inside quotes, like "5", JavaScript treats it as text (a string), not as a mathematical number.

Basic math operators: +, -, *, /

To do math in JavaScript, we use special symbols called operators. Here are the four basic ones you will use most often:

  • + (Addition): Adds two numbers together.
  • - (Subtraction): Subtracts one number from another.
  • * (Multiplication): Multiplies two numbers. Notice we use an asterisk * instead of a traditional math x.
  • / (Division): Divides one number by another using a forward slash.

There are more math tools in JavaScript, but this lesson focuses only on the four basic operators.

First code example

Let's start with one simple calculation. This code multiplies a price by a quantity to calculate a total:

Can edit
const price = 10;
const quantity = 3;
const total = price * quantity;

console.log(total);

Step-by-step explanation of the code

Let's break down exactly what the computer is doing in the code above:

  • First, we create a variable called price and store the number 10.
  • Next, we create a variable called quantity and store the number 3.
  • Then, we multiply price by quantity using the * operator and store the result in total.
  • When we use console.log(total), JavaScript sends the final result (30) to the output section below the code.

Mini task

Try it yourself. Create a variable called score and give it a value of 50. Then create a variable called bonus with a value of 10. Finally, create a third variable called finalScore that adds score and bonus together.

Click Run and check the output section below the code.

Can edit
// put your code below

console.log()

Short quiz

Question 1: Which symbol is used for multiplication in JavaScript? A) x B) * C) #

Question 2: How should you write the number twenty in your code so that JavaScript can do math with it? A) "20" B) twenty C) 20

(Answers: 1 - B, 2 - C)

Small challenge

Imagine you are baking cookies. You have 24 cookies and want to share them equally among 4 friends. Write JavaScript code using variables and the division operator (/) to calculate how many cookies each friend gets.

Click Run and check the output section below the code.

Can edit
// put your code below

console.log()

Summary

  • Numbers are used for math and calculating numerical data in JavaScript.
  • Write numbers without quotes (e.g., 10, not "10") so the computer knows they are numbers and not text.
  • Use + to add, - to subtract, * to multiply, and / to divide.
  • You can easily store numbers in variables and do math with them to build practical programs!