JavaScript: Numbers
Owner: Prof I Siebörger
Modifier: Dr M Moyo
Information Systems - 2025
Overview
Understand numeric Explore arithmetic
data types operators
Numeric values
Has decimal point
• JavaScript only has one type of number
◦ always stored as double precision floating point numbers 3 stored as 3.0
◦ accurate up to 15 digits GJ bit
◦ maximum number of decimals is 17
• Numbers can be written with, or without, decimals
Assigning a value to a variable
• For a numeric value, the value assigned can be:
◦ a number (e.g. 100), or
let age = 100;
◦ it can be a numeric expression (e.g. 33 + G7)
let total = 33 + 67;
• Numeric values: Statement that evaluates to a value
◦ is simply a number
◦ cannot contain a letter, except for the letter e or
E, which is used in exponential notation
◦ cannot contain special symbols, such as the % sign, the $ sign, or the comma
◦ cannot be enclosed in quotation marks (“”)
Numeric values
Valid:
let coins = 35;
let population = 100245;
let units = 0.25;
let results = 12.3;
Invalid:
let coins = 35c;
let population = “100245”; It is a valid String, however!
let units = 0,25;
let results = 12.3%;
Assigning a value to a variable
value
let apples = 5;
let oranges = 10;
numeric expressions
statement let fruit = apples + oranges;
variable operator
JS Number Methods
Converting Variables to Numbers
Number Object Methods
The toString() method
• fieturns a number as a string
• All number methods return a new value. They do not change the
original variable
The toFixed() method
• fieturns a number as a string
• The number is written with a specified number of decimals
Arithmetic operators (1)
• An operator is a symbol that tells the computer to perform a specific
mathematical or logical manipulation
◦ Below are binary operators (require operand before and after the operator)
Operator Description Expression Returns
+ Combines or adds two items 12 + 3 15
– Subtracts one item from another 12 – 3 9
* Multiplies two items 12*3 3G
/ Divides one item by another 12/3 J
% fieturns the remainder (modulus) after dividing one integer 18%5 3
by another integer
** fiaising a value to a power 3**2 9
Arithmetic operators (2)
• Operator precedence:
Brackets are always the highest
1 ++ increment
highest -- decrement
2 * multiplication
/ division
% modulus
3 + addition
lowest
- subtraction
• Apply left-to-right rule for equal precedence operators
Arithmetic operators: Modulus
What’s the
deal with % ?
3%3 = 0
Cives the remainder of 4%3 = 1
the division 8%3 = 2
Arithmetic calculation example 1
•12 + 25 – 4 * 2
•12 + 25 – 8
•37 – 8
•29
Arithmetic calculation example 2
• 4 * (4 + 5) – 5 * 6 + 2
•4 * 9 – 5 * 6 + 2
• 36 – 5 * 6 + 2
• 36 – 30 + 2
•6 + 2
•8
Arithmetic calculation example 3
•4 * 2 % 3 + 5 % 2
•8 % 3 + 5 % 2
1 ++ increment
•2 + 5 % 2 -- decrement
2 * multiplication
•2 + 1 / division
% modulus
3 +
•3 -
addition
subtraction
Floating point remainder
• You can apply modulus operator (%) to floating point values too
• fiesult will be a floating-point remainder
12.6 % 5.1 // result 2.4
12.6 – 5.1 = 7.5
7.5 – 5.1 = 2.4
Errors in integer calculations
• Divide by Zero:
◦ in situations such as division by 0, infinity (Infinity) is returned
let x = 12 / 0;
This produces: Infinity
Arithmetic operators (3)
• Unary operator: requires just a single operand either before or after the
operator
Operator Description Expression Returns
++ Increases a value by 1 12++ 13
–– Decreases a value by 1 12– – 11
– Changes the sign of a value –12 –12
Increment anddecrement
• Increment operator (++): let count = 10;
count++; //adds one to count (11)
◦ increase value by 1
same as: count = count + 1
• Decrement operator (-- ): let count = 10;
◦ decrease value by 1 count--; //subtracts one from count (9)
same as: count = count - 1
Logical Operator
AND Truth Table
OR Truth Table
NOT Truth Table
Using Operators to Build Expressions
• Assignment operators
◦ An assignment operator (e.g., =) is used for assigning a value to a variable
◦ Compound assignment operators both assign a value and perform a calculation
◦ Interpreter will attempt to convert a nonnumeric to a numeric operand
Operator Example Equivalent to
= x=y x=y
+= x += y x=x+y
–= x –= y x=x–y
*= x *= y x=x*y
/= x /= y x = x/y
%= x %= y x= x %y
**= x **= y x = x**y
Using Operators to Build Expressions
• Comparison operators (relational operators): used to compare two operands
◦ Two non-numerical operands are compared in lexicographical order
◦ String plus number: interpreter converts string to number or returns false
Operator Example Description
== x == y Tests whether x is equal in value to y
=== x === y Tests whether x is equal in value to y and has the same data type
!= x != y Tests whether x is not equal to y or has a different data type
!== x !== y Tests whether x is not equal to y and/or doesn’t have the same data type
> x>y Tests whether x is greater than y
>= x >= y Tests whether x is greater than or equal to y
< x<y Tests whether x is less than y
<= x <= y Tests whether x is less than or equal to y
Using Operators to Build Expressions
• Logical operators
◦ Used to combine expressions that will result in a Boolean value of true or false
◦ Used for negating (swapping) a Boolean value
◦ Multiple conditions can be grouped within parentheses to create more complicated
statements
Operator Definition Example Description
&& and (x === 5) && (y === 8) Tests whether x is equal to 5 and y is
equal to 8
|| or (x === 5) || (y === 8) Test whether x is equal to 5 or y is equal
to 8
! not ! (x < 5) Test whether x is not less than 5
Taking
input from
the user
• A prompt box is used if you want the
user to input a value.
• When a prompt box pops up, the user
will have to click either "OK" or
"Cancel" to proceed.
• The prompt() method displays a
dialog box that prompts the user for
input.
◦ Input from the user stored as a
string
◦ Use number() to convert strings
to numbers
Example
• Calculate maximum heart rate while training
◦ maximum number of times your heart should beat per minute during exercise
• Formulae:
◦ Max heart rate = 220 - your age
Example