Summarize the common properties of BigIn functions in JavaScript

Summarize the common properties of BigIn functions in JavaScript

1. Overview

BigInt is a special numeric type that provides support for integers of arbitrary length.

There are two ways to create bigint : add n after an integer literal or call the BigInt function, which generates bigint from a string, number, etc.

const bigint = 1234567890123456789012345678901234567890n; 
const sameBigint = BigInt("1234567890123456789012345678901234567890"); 
const bigintFromNumber = BigInt(10); // Same as 10n

2. Attributes

1. Mathematical operators

BigInt can be used like regular numeric types in most cases.

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 bigint return results that are also bigint.

You cannot mix bigint and regular numeric types:

alert(1n + 2); // Error: Cannot mix BigInt and other types 

If necessary, you should convert them explicitly: using BigInt() or Number(), like this:

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.

BigInt does not support unary addition

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 bigint :

let bigint = 1n; 
alert( +bigint ); // error 

Number() should be used to convert a bigint to a number type.

2. Comparison Operators

Comparison operators, such as < and >, have no problem using them to compare bigint and number types.

alert( 2n > 1n ); // true 
alert( 2n > 1 ); // true 

Note: Because number and bigint are different types, they may be equal when compared with ==, but not equal when compared with === (strict equality):

alert( 1 == 1n ); // true 
 
alert( 1 === 1n ); // false 

3. Boolean operations

When 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 JSBI instead of native bigint when writing code. But JSBI uses number internally like bigint and emulates it as closely as possible to the specification, so the code is already bigint -ready.

For engines that don't support bigint , such JSBI code can be used "as is", for those that do — the polyfill will convert the calls to native bigint.

Conclusion

Based on JavaScript basics, this article introduces BigInt functions, common properties, and comparison of numeric operators through BigInt functions. Boolean operations, etc., are explained in detail through case analysis.

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:
  • Detailed explanation of BigInt for digital calculations beyond JavaScript safe integer limits

<<:  Use non-root users to execute script operations in docker containers

>>:  Implementation of form submission in html

Recommend

Building a Redis cluster on Docker

Table of contents 1. Pull the image 2. Create a R...

Some lesser-known sorting methods in MySQL

Preface ORDER BY 字段名升序/降序, I believe that everyon...

Use of Linux ifconfig command

1. Command Introduction The ifconfig (configure a...

Installation tutorial of MySQL 5.1 and 5.7 under Linux

The operating system for the following content is...

Notes on using the blockquote tag

<br />Semanticization cannot be explained in...

How to allow remote access to open ports in Linux

1. Modify the firewall configuration file # vi /e...

In-depth analysis of MySQL explain usage and results

Preface In daily work, we sometimes run slow quer...

How to delete folders, files, and decompress commands on Linux servers

1. Delete folders Example: rm -rf /usr/java The /...

Linux uses dual network card bond and screwdriver interface

What is bond NIC bond is a technology that is com...

Tomcat8 uses cronolog to split Catalina.Out logs

background If the catalina.out log file generated...

Detailed explanation of the process of installing MySQL on Ubuntu 18.04.4

Let's take a look at the process of installin...

Methods and steps to build nginx file server based on docker

1. Create a new configuration file docker_nginx.c...