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

Summary of some tips on MySQL index knowledge

Table of contents 1. Basic knowledge of indexing ...

Three ways to implement animation in CSS3

This is a test of the interviewee's basic kno...

Appreciation of the low-key and elegant web design in black, white and gray

Among classic color combinations, probably no one...

jQuery uses the canvas tag to draw the verification code

The <canvas> element is designed for client...

Detailed explanation of lazy loading and preloading of webpack

Table of contents Normal loading Lazy Loading Pre...

Linux centOS installation JDK and Tomcat tutorial

First download JDK. Here we use jdk-8u181-linux-x...

How to mount a disk in Linux

When using a virtual machine, you may find that t...

How to solve the mysql insert garbled problem

Problem description: When inserting Chinese chara...

How to install and modify the initial password of mysql5.7.18 under Centos7.3

This article shares with you the installation of ...

Can Docker become the next "Linux"?

The Linux operating system has revolutionized the...

Tutorial on installing Apache 2.4.41 on Windows 10

1. Apache 2.4.41 installation and configuration T...

What are the differences between sql and mysql

What is SQL? SQL is a language used to operate da...

Web Design Tutorial (6): Keep your passion for design

<br />Previous article: Web Design Tutorial ...

Quickly solve the problem that CentOS cannot access the Internet in VMware

Yesterday I installed CentOS7 under VMware. I wan...