DEV Community

Tshegofatso Letlape
Tshegofatso Letlape

Posted on

Arithmetic Adventures

In this quick read, I journey through key concepts of arithmetic and numbers in JavaScript!

1.Basic Arithmetic Operations
The fundamental arithmetic operators (+, -, , /) are your tools for addition, subtraction, multiplication, and division. But there's more: the modulo operator (%) helps you find remainders, and the exponentiation operator (*) raises the first operand to the power of the second operand.

2.Math Object
Meet the Math object, your trusty sidekick for complex mathematical functions. From Math.sqrt() for square roots to Math.random() for random numbers, this object has your back when the math gets tricky.

3.Numbers in JavaScript
Numbers in JavaScript are mostly represented as floating-point numbers, which can occasionally lead to precision issues in calculations. The toFixed() method helps control decimal places, ensuring you have the precision you need.

4. NaN and Infinity
NaN(Not-a-Number) pops up when a mathematical operation doesn't yield a valid number, and Infinity appears when numbers get too large or too small to be represented.

5. Converting Strings to Numbers
JavaScript offers parseInt() and parseFloat() functions which turns string inputs into their numeric counterparts.

6. Number Methods and Properties
Numbers in JavaScript come with a handy set of methods and properties. Whether it's Number.isNaN() to check if a value is NaN or Number.MAX_VALUE to access the largest possible value, these tools are important.

7. Number Precision
Floating-point numbers may lead to tiny discrepancies in calculations due to their representation in memory. So when you're dealing with important financial computations or precise measurements, be careful and consider using methods like Number.EPSILON for comparison purposes.

Happy coding and calculating.

Top comments (0)