JS Numbers
JavaScript numbers are a fundamental part of the language, and understanding them is crucial for writing effective and efficient code. They’re more than just raw numerical values; they represent data in a way that allows JavaScript to perform calculations, manipulate objects, and work with various data types. This tutorial will cover the core concepts of JavaScript numbers, providing a solid foundation for further exploration.
Introduction
JavaScript numbers are represented as a single-digit number with a leading zero. They are used to store numerical values, and they are essential for many operations within JavaScript. Unlike some other programming languages, JavaScript numbers are always treated as integers, meaning they can only be whole numbers. This is a key difference and influences how you approach calculations and data manipulation.
Basic Number Types
JavaScript offers several built-in number types:
- Number: This is the most basic type, representing whole numbers. It's the default type for numbers in JavaScript.
- BigInt: This type represents integers that are larger than the maximum safe integer value. It's crucial for handling very large numbers that might exceed the capacity of a regular
Numbertype. It's available in ES2020 and later. - Decimal: Represents numbers with a decimal point (e.g., 3.14).
Operations with Numbers
JavaScript provides a variety of operators to work with numbers:
- Addition (+):
5 + 3results in8 - Subtraction (-):
10 - 2results in8 - Multiplication (*):
5 * 3results in15 - Division (/):
10 / 2results in5 - Modulo (%):
10 % 3results in1(the remainder after division) - Exponentiation () :**
2 ** 3results in8
Working with BigInt
BigInt is essential for working with numbers that exceed the maximum safe integer value. Here's how to use it:
let largeNumber = 12345678901234567890;
let bigNumber = BigInt(largeNumber);
console.log(bigNumber); // Output: 12345678901234567890
Practice Exercises
Let's test your understanding with a few exercises:
- Calculate the sum of two numbers:
15 + 7 - Calculate the product of two numbers:
25 * 4 - Calculate the difference between two numbers:
80 - 12 - Calculate the remainder when dividing 100 by 3:
100 % 3 - Calculate the square of a number:
9 * 3
💡 Tip: When working with BigInt, be mindful of potential overflow issues. If you're dealing with very large numbers, consider using a library like big.js for more robust handling.
Summary
This tutorial has introduced you to the core concepts of JavaScript numbers. Understanding the different number types (Number, BigInt) and how to perform basic arithmetic operations is fundamental to writing effective JavaScript code. Remember to leverage BigInt when you need to work with numbers that exceed the safe integer limits. Experiment with the practice exercises to solidify your knowledge.
Further Exploration
For more in-depth learning, consider exploring:
- JavaScript Number Methods: Learn about techniques like long division and other methods for handling very large numbers.
- BigInt Library: Explore the
big.jslibrary for more advanced BigInt operations. - Number Formatting: Learn how to format numbers for display in JavaScript.