In JavaScript, numbers are represented as 64-bit floating point values, also known as “double-precision” numbers. This means that all numbers in JavaScript are stored as decimals, even if they appear to be integers.
You can create a number in JavaScript by simply typing a numeric literal:
let x = 10;
You can perform arithmetic operations on numbers using the standard mathematical operators:
let x = 10;
let y = 20;
let sum = x + y;
let difference = x - y;
let product = x * y;
let quotient = x / y;
let remainder = x % y;
You can also use the Math object in JavaScript to perform more complex mathematical operations, such as trigonometric functions, logarithms, and exponentiation:
let x = 2;
let y = 3;
let power = Math.pow(x, y); //Equivalent to x ** y
let squareRoot = Math.sqrt(x);
let sine = Math.sin(y);
let logarithm = Math.log(x);
JavaScript also includes several special values for numbers, including NaN (Not a Number), Infinity, and -Infinity. These values can be useful for handling errors and edge cases in your code.
let x = "hello";
let y = 0;
let result = x / y;
console.log(result); // Output:NaN
let max = Number.MAX_VALUE;
let infinity = Infinity;
let negativeInfinity =-Infinity;
Overall, numbers are a fundamental data type in JavaScript, and understanding how to work with them is essential for building complex applications.
BigInt in JavaScript:
In JavaScript, BigInt is a new numeric primitive type that was introduced in ECMAScript 2020. It is designed to handle very large integers, which cannot be accurately represented using the standard Number type.
To create a BigInt, you append the n suffix to a numeric literal or use the BigInt() constructor:
let bigNumber =123456789012345678901234567890n;
let anotherBigNumber =
BigInt("12345678901234567890123456780");
You can perform arithmetic operations on BigInt values using the standard mathematical operators:
let x =
123456789012345678901234567890n;
let y =9876543210987654321098765432n;
let sum = x + y;
let difference = x - y;
let product = x * y;
let quotient = x / y; // Roundeddown to nearest integer
let remainder = x % y;
However, you cannot mix BigInt values with Number values in arithmetic operations. If you need to convert a BigInt to a Number or vice versa, you can use the Number() or BigInt() constructor:
let bigNumber =
BigInt("12345678901234567890123456780");
let number = Number(bigNumber);
let anotherNumber =123456789012345678901234567890;
let anotherBigNumber =BigInt(anotherNumber);
BigInt values can also be compared using the standard comparison operators:
let x = 12345678901234567890123456790n;
let y =98765432109876543210987654321n;
console.log(x > y); //Output: true
console.log(x === y); // Output:false
console.log(x < y); //Output: false
Overall, BigInt provides a useful extension to the standard numeric types in JavaScript, allowing developers to work with very large integers more accurately and efficiently.
JavaScript Number Methods:
JavaScript provides several built-in methods for working with numbers. Here are some of the most commonly used methods:
Number.toString() – converts a number to a string with the specified base (default is base 10).
let x = 123;
console.log(x.toString()); //Output: "123"
console.log(x.toString(2)); //
Output: "1111011" (binaryrepresentation)
let x = 123.456;
console.log(x.toFixed(2)); //Output: "123.46"
let x = 123.456;
console.log(x.toPrecision(4));// Output: "123.5"
console.log(x.toPrecision(6));// Output: "123.456"
let x = "123";
console.log(Number.parseInt(x));// Output: 123
let x = "123.456";
console.log(Number.parseFloat(x));// Output: 123.456
console.log(Number.isInteger(123));// Output: true
console.log(Number.isInteger(123.456));// Output: false
console.log(Number.isNaN(NaN));// Output: true
console.log(Number.isNaN("hello"));// Output: false
console.log(Number.isFinite(123));// Output: true
console.log(Number.isFinite(Infinity));// Output: false
These methods are just a few examples of the many ways you can work with numbers in JavaScript. By mastering these methods, you can write more efficient and effective code when working with numeric data.
Number Properties:
In JavaScript, there are several number properties that you can use to perform various operations. Here are some of the most commonly used number properties:
Number.MAX_VALUE – returns the largest representable number (i.e., the maximum value that can be represented in JavaScript).
console.log(Number.MAX_VALUE);// Output: 1.7976931348623157e+308
console.log(Number.MIN_VALUE);// Output: 5e-324
console.log(Number.NaN); //Output: NaN
console.log(Number.POSITIVE_INFINITY);// Output: Infinity
console.log(Number.NEGATIVE_INFINITY);// Output: -Infinity
By using these number properties, you can perform various operations and comparisons on numbers in JavaScript.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.