<!DOCTYPE html>
Day 8: Cracking Numbers and Math in JavaScript
<br> body {<br> font-family: sans-serif;<br> line-height: 1.6;<br> margin: 20px;<br> }</p> <div class="highlight"><pre class="highlight plaintext"><code> h1, h2, h3 { color: #333; } code { background-color: #eee; padding: 2px 5px; border-radius: 3px; } pre { background-color: #f5f5f5; padding: 10px; border-radius: 5px; overflow-x: auto; } </code></pre></div> <p>
Day 8: Cracking Numbers and Math in JavaScript
Welcome back! Today, we'll dive into the world of numbers and mathematical operations in JavaScript. This essential foundation allows us to perform calculations, manipulate data, and build dynamic and interactive applications.
- Number Data Type
In JavaScript, numbers are represented using the
number
data type. This includes both integers (whole numbers) and floating-point numbers (numbers with decimal points).
let integer = 10;
let float = 3.14;
- Addition (+): Adds two numbers.
- Subtraction (-): Subtracts one number from another.
- Multiplication (): Multiplies two numbers.
- Division (/): Divides one number by another.
- Modulus (%): Returns the remainder of a division operation.
- Exponentiation (): Raises a number to a power.
let sum = 5 + 3; // 8
let difference = 10 - 4; // 6
let product = 2 * 7; // 14
let quotient = 15 / 3; // 5
let remainder = 10 % 3; // 1
let power = 2 * 3; // 8
JavaScript provides a built-in
Math
object that offers a wide range of mathematical functions and constants:
-
Math.abs(x)
: Returns the absolute value ofx
. -
Math.ceil(x)
: Roundsx
up to the nearest integer. -
Math.floor(x)
: Roundsx
down to the nearest integer. -
Math.max(x1, x2, ...)
: Returns the largest of the given numbers. -
Math.min(x1, x2, ...)
: Returns the smallest of the given numbers. -
Math.pow(x, y)
: Raisesx
to the power ofy
. -
Math.round(x)
: Roundsx
to the nearest integer. -
Math.sqrt(x)
: Returns the square root ofx
. -
Math.PI
: Represents the mathematical constant pi (π). -
Math.E
: Represents the mathematical constant e (Euler's number).
let absoluteValue = Math.abs(-5); // 5
let roundedNumber = Math.round(3.7); // 4
let maximum = Math.max(10, 25, 5); // 25
let squareRoot = Math.sqrt(16); // 4
You can convert strings to numbers using the
parseInt()
and
parseFloat()
functions.
let stringNumber = "123";
let integerNumber = parseInt(stringNumber); // 123
let floatNumber = parseFloat("3.14"); // 3.14
To format numbers for display, you can use the
toFixed()
method to control the number of decimal places:
let number = 3.14159;
let formattedNumber = number.toFixed(2); // "3.14"
Let's put our knowledge to use by calculating the area of a circle using JavaScript.
function calculateCircleArea(radius) {
let area = Math.PI * Math.pow(radius, 2);
return area;
}
let radius = 5;
let circleArea = calculateCircleArea(radius);
console.log("Area of the circle:", circleArea);
This code defines a function
calculateCircleArea
that takes the radius as input, calculates the area using the formula (π * radius²), and returns the result. The example then calls the function with a radius of 5 and prints the area to the console.
- Conclusion
Understanding numbers and mathematical operations is essential for any JavaScript developer. This foundation enables you to create sophisticated calculations, analyze data, and build interactive applications. Remember to use the Math object for common mathematical functions and constants, and leverage the conversion and formatting capabilities to manipulate numbers effectively. Keep practicing, and you'll become a number-crunching master in no time!