Dive into the world of numbers and mathematical operations in JavaScript, exploring numeric literals, arithmetic operations, operator precedence, and the powerful Math object.
In this section, we will explore how JavaScript handles numbers and mathematical operations. Understanding these concepts is crucial as they form the foundation for performing calculations and manipulating data in your programs. Let’s dive into the world of numbers in JavaScript!
In JavaScript, numbers are represented as numeric literals. A numeric literal is a fixed value that you can use directly in your code. JavaScript supports several types of numeric literals:
42
, -7
, 0
.3.14
, -0.001
, 2.0
.1.23e4
represents 1.23 × 10^4
.Here’s how you can use numeric literals in JavaScript:
let integerLiteral = 42; // An integer
let floatingPointLiteral = 3.14; // A floating-point number
let exponentialLiteral = 1.23e4; // Exponential notation
JavaScript provides a set of arithmetic operators that allow you to perform basic mathematical operations. Let’s explore these operators:
+
)The addition operator adds two numbers together.
let sum = 5 + 3; // 8
-
)The subtraction operator subtracts one number from another.
let difference = 10 - 4; // 6
*
)The multiplication operator multiplies two numbers.
let product = 6 * 7; // 42
/
)The division operator divides one number by another.
let quotient = 20 / 5; // 4
%
)The modulus operator returns the remainder of a division operation.
let remainder = 10 % 3; // 1
Operator precedence determines the order in which operations are performed in an expression. In JavaScript, multiplication and division have higher precedence than addition and subtraction. You can use parentheses to override the default precedence and ensure that operations are performed in the desired order.
For example:
let result = 5 + 3 * 2; // 11, because multiplication is performed first
let resultWithParentheses = (5 + 3) * 2; // 16, because addition is performed first
JavaScript provides a built-in Math
object that contains a collection of properties and methods for performing mathematical operations. Let’s explore some of the most commonly used methods:
Math.random()
The Math.random()
method returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).
let randomNumber = Math.random();
console.log(randomNumber); // A random number between 0 and 1
Math.floor()
The Math.floor()
method rounds a number down to the nearest integer.
let roundedDown = Math.floor(4.7); // 4
Math.ceil()
The Math.ceil()
method rounds a number up to the nearest integer.
let roundedUp = Math.ceil(4.1); // 5
Math.round()
The Math.round()
method rounds a number to the nearest integer.
let rounded = Math.round(4.5); // 5
Math.max()
and Math.min()
The Math.max()
method returns the largest of zero or more numbers, while Math.min()
returns the smallest.
let maxNumber = Math.max(3, 7, 2); // 7
let minNumber = Math.min(3, 7, 2); // 2
Math.pow()
The Math.pow()
method returns the base to the exponent power, that is, base^exponent.
let power = Math.pow(2, 3); // 8
Math.sqrt()
The Math.sqrt()
method returns the square root of a number.
let squareRoot = Math.sqrt(16); // 4
Now that we’ve covered the basics of numbers and mathematical operations in JavaScript, let’s try a few exercises to reinforce what we’ve learned. Experiment with the following code snippets and modify them to see different results:
area = π * radius^2
. Use Math.PI
for the value of π.let radius = 5;
let area = Math.PI * Math.pow(radius, 2);
console.log("Area of the circle:", area);
let randomInt = Math.floor(Math.random() * 10) + 1;
console.log("Random integer between 1 and 10:", randomInt);
let numbers = [3, 7, 2, 9, 5];
let max = Math.max(...numbers);
let min = Math.min(...numbers);
console.log("Maximum value:", max);
console.log("Minimum value:", min);
In this section, we’ve explored how JavaScript handles numbers and mathematical operations. We learned about numeric literals, arithmetic operators, operator precedence, and the powerful Math
object. These concepts are fundamental to performing calculations and manipulating data in your JavaScript programs.
For more information on numbers and mathematical operations in JavaScript, check out the following resources: