1. Overview There are two ways to create const bigint = 1234567890123456789012345678901234567890n; const sameBigint = BigInt("1234567890123456789012345678901234567890"); const bigintFromNumber = BigInt(10); // Same as 10n 2. Attributes1. Mathematical operators For example: alert(1n + 2n); // 3 alert(5n / 2n); // 2 Note: The result of the division 5/2 is rounded towards zero, and the result obtained after rounding has no decimal part. All operations on You cannot mix alert(1n + 2); // Error: Cannot mix BigInt and other types If necessary, you should convert them explicitly: using let bigint = 1n; let number = 2; // Convert number to bigint alert(bigint + BigInt(number)); // 3 // Convert bigint to number alert(Number(bigint) + number); // 3 The conversion is always silent and never raises an error, but if the bigint is too large to fit in the numeric type, the extra bits will be truncated, so such conversions should be done with caution. The unary addition operator +value is a well-known method for converting value to a numeric type. To avoid confusion, unary addition is not supported in let bigint = 1n; alert( +bigint ); // error
2. Comparison Operators Comparison operators, such as < and >, have no problem using them to compare alert( 2n > 1n ); // true alert( 2n > 1 ); // true Note: Because alert( 1 == 1n ); // true alert( 1 === 1n ); // false 3. Boolean operationsWhen inside an if or other Boolean operation, bigint behaves like number. Example: In if, bigint 0n is false, and other values are true: if (0n) { // will never be executed} Boolean operators For example , ||, && and other operators handle bigint in a similar way to number: alert( 1n || 2 ); // 1 (1n is considered true) alert( 0n || 2 ); // 2 (0n is considered false) Note: This method recommends using For engines that don't support Conclusion Based on This concludes this article on summarizing the common properties of the BigIn function in JavaScript. For more information about the common properties of the BigIn function in JavaScript, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Use non-root users to execute script operations in docker containers
>>: Implementation of form submission in html
Table of contents 1. Basic knowledge of indexing ...
Today I have nothing to do, so I listed some tool...
This is a test of the interviewee's basic kno...
Among classic color combinations, probably no one...
The <canvas> element is designed for client...
Table of contents Normal loading Lazy Loading Pre...
First download JDK. Here we use jdk-8u181-linux-x...
When using a virtual machine, you may find that t...
Problem description: When inserting Chinese chara...
This article shares with you the installation of ...
The Linux operating system has revolutionized the...
1. Apache 2.4.41 installation and configuration T...
What is SQL? SQL is a language used to operate da...
<br />Previous article: Web Design Tutorial ...
Yesterday I installed CentOS7 under VMware. I wan...