JavaScript Interview Question #44: Number vs BigInt in JS

What happens if we add an n suffix to a regular number in JavaScript? What’s the output?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In the first line we try to add two numbers. These aren’t regular numbers, but rather two instances of BigInt…


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer

javascript interview question #44

What happens if we add an n suffix to a regular number in JavaScript? What’s the output?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In the first line we try to add two numbers. These aren’t regular numbers, but rather two instances of BigInt — special objects that are used to safely represent numbers bigger than Number.MAX_SAFE_INTEGER.

There are two ways to create BigInt:

  • add a suffix n to any number in JavaScript
  const big = 1000000n; // 1000000n
  • call the constructor BigInt(val) and pass in a numerical value
  const bigN = BigInt(123) // 123n

This value doesn’t have to a number. I can be a string.

  const bigS = BigInt("234") // 234n

You can also use hex and binary notation.

  const bigHex = BigInt("0xffffffffffffffff") // 18446744073709551615n
  const bigBin = BigInt("0b111") // 7n

The BigInt numbers behave just like the regular ones. By adding 1n and 2n we get 3n. This is BigInt as well, and typeof 3n returns a string bigint, which will be logged to the screen when we call console.log.

ANSWER: The n suffix turns a regular JavaScript number into a BigInt. The string bigint will be logged to the console.

Learn Full-Stack JavaScript


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer


Print Share Comment Cite Upload Translate Updates
APA

Coderslang: Become a Software Engineer | Sciencx (2021-05-31T15:51:14+00:00) JavaScript Interview Question #44: Number vs BigInt in JS. Retrieved from https://www.scien.cx/2021/05/31/javascript-interview-question-44-number-vs-bigint-in-js/

MLA
" » JavaScript Interview Question #44: Number vs BigInt in JS." Coderslang: Become a Software Engineer | Sciencx - Monday May 31, 2021, https://www.scien.cx/2021/05/31/javascript-interview-question-44-number-vs-bigint-in-js/
HARVARD
Coderslang: Become a Software Engineer | Sciencx Monday May 31, 2021 » JavaScript Interview Question #44: Number vs BigInt in JS., viewed ,<https://www.scien.cx/2021/05/31/javascript-interview-question-44-number-vs-bigint-in-js/>
VANCOUVER
Coderslang: Become a Software Engineer | Sciencx - » JavaScript Interview Question #44: Number vs BigInt in JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/31/javascript-interview-question-44-number-vs-bigint-in-js/
CHICAGO
" » JavaScript Interview Question #44: Number vs BigInt in JS." Coderslang: Become a Software Engineer | Sciencx - Accessed . https://www.scien.cx/2021/05/31/javascript-interview-question-44-number-vs-bigint-in-js/
IEEE
" » JavaScript Interview Question #44: Number vs BigInt in JS." Coderslang: Become a Software Engineer | Sciencx [Online]. Available: https://www.scien.cx/2021/05/31/javascript-interview-question-44-number-vs-bigint-in-js/. [Accessed: ]
rf:citation
» JavaScript Interview Question #44: Number vs BigInt in JS | Coderslang: Become a Software Engineer | Sciencx | https://www.scien.cx/2021/05/31/javascript-interview-question-44-number-vs-bigint-in-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.