Number
JavaScript Number Tutorial
Table of Contents
Introduction to JavaScript Number
JavaScript Number Types
Integer
Floating-Point Numbers
Exponential Notation
Infinity and -Infinity
NaN (Not-a-Number)
JavaScript Number Methods
toString()
toFixed()
toPrecision()
toExponential()
valueOf()
Number()
parseInt()
parseFloat()
JavaScript Number Properties
MAX_VALUE
MIN_VALUE
POSITIVE_INFINITY
NEGATIVE_INFINITY
NaN
Number Conversions
Converting Strings to Numbers
Converting Numbers to Strings
Checking if a Value is a Number
isNaN()
isFinite()
Number.isInteger()
Working with Large Numbers
BigInt
Real-World Applications of JavaScript Number
Conclusion
1. Introduction to JavaScript Number
Welcome to the Codes with Pankaj tutorial on JavaScript Number! In this tutorial, we'll explore how numbers work in JavaScript. You'll learn about different types of numbers, methods, and properties available in the Number
object. Let's get started!
In JavaScript, numbers are a fundamental data type used to represent both integers and floating-point values. Unlike some programming languages, JavaScript does not differentiate between integers and floats; both are treated as Number
data types.
2. JavaScript Number Types
JavaScript supports several types of numbers:
Integer
An integer is a whole number, positive or negative, without decimals.
Example:
Floating-Point Numbers
A floating-point number, or float, is a number that contains a decimal point.
Example:
Exponential Notation
JavaScript allows numbers to be written in exponential notation, where e
represents "times ten raised to the power of."
Example:
Infinity and -Infinity
JavaScript represents infinity with the Infinity
and -Infinity
values. These occur when a number exceeds the maximum or minimum limit.
Example:
NaN (Not-a-Number)
NaN
represents a value that is not a legal number. It occurs when you try to perform an invalid mathematical operation.
Example:
3. JavaScript Number Methods
JavaScript provides several methods for working with numbers:
toString()
Converts a number to a string.
Example:
toFixed()
Formats a number to a fixed number of decimal places.
Example:
toPrecision()
Formats a number to a specified length.
Example:
toExponential()
Converts a number to exponential notation.
Example:
valueOf()
Returns the primitive value of a number object.
Example:
Number()
Converts a value to a number.
Example:
parseInt()
Parses a string and returns an integer.
Example:
parseFloat()
Parses a string and returns a floating-point number.
Example:
4. JavaScript Number Properties
The Number
object includes several properties that represent mathematical constants:
MAX_VALUE
Represents the largest possible number in JavaScript.
Example:
MIN_VALUE
Represents the smallest possible number in JavaScript.
Example:
POSITIVE_INFINITY
Represents positive infinity.
Example:
NEGATIVE_INFINITY
Represents negative infinity.
Example:
NaN
Represents the value NaN
.
Example:
5. Number Conversions
JavaScript allows you to convert values between numbers and strings.
Converting Strings to Numbers
You can convert strings to numbers using Number()
, parseInt()
, or parseFloat()
.
Example:
Converting Numbers to Strings
You can convert numbers to strings using the toString()
method.
Example:
6. Checking if a Value is a Number
JavaScript provides functions to check if a value is a valid number.
isNaN()
Checks if a value is NaN
.
Example:
isFinite()
Checks if a value is a finite number.
Example:
Number.isInteger()
Checks if a value is an integer.
Example:
7. Working with Large Numbers
JavaScript introduced BigInt
to handle very large numbers beyond the safe integer range.
Example:
8. Real-World Applications of JavaScript Number
JavaScript numbers are widely used in web development for:
Calculations and Data Processing: Performing arithmetic operations, financial calculations, and more.
Form Validation: Ensuring that inputs like age, price, and quantity are valid numbers.
Game Development: Using numbers for scores, positions, and physics calculations.
9. Conclusion
In this detailed tutorial, we explored the JavaScript Number
object and its methods and properties. You learned how to work with different types of numbers, perform conversions, and check if a value is a valid number.
For more tutorials and examples, visit www.codeswithpankaj.com! Happy coding!
Last updated