
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Using BigInt to calculate long factorials in JavaScript
We are required to write a JavaScript function that takes in a number as the only input. The function should calculate the factorial of big numbers (greater than 10) whose factorial can be accommodated in the simple let or type variables using the new bigInt variable of JavaScript. Lastly the function should convert the factorial to a string and return the string.
For example − If the input is −
const num = 45;
Then the output should be −
const output = '119622220865480194561963161495657715064383733760000000000';
Example
The code for this will be −
const num = 45; const longFactorial = (num) => { var bigInt = BigInt(num); var factorial = 1n; for (let i = 0n; i < bigInt ; i++) { factorial *= bigInt − i; } return String(factorial); } console.log(longFactorial(45));
Output
And the output in the console will be −
119622220865480194561963161495657715064383733760000000000
- Related Articles
- BigInt in JavaScript
- C++ Program to Compute Combinations using Factorials
- Difference between BIGINT and BIGINT(20) in MySQL?
- Find last two digits of sum of N factorials using C++.
- How to calculate the date three months prior using JavaScript?
- How to calculate the XOR of array elements using JavaScript?
- How to calculate days left until next Christmas using JavaScript?
- How to use BIGINT(8)value in Android sqlite?
- Product of first N factorials in C++
- Find the unit place digit of sum of N factorials using C++.
- Divisors of factorials of a number in java
- Find sum of factorials in an array in C++
- How do I cast a type to a BigInt in MySQL?
- MySQL BigInt zerofill vs int zerofill?
- How to calculate and print bonus and gross using basic salary by JavaScript?

Advertisements