How to find factorial of a number in JavaScript?17 Mar 2025 | 3 min read Factorial of number is the product of all positive descending integers. Factorial of n is denoted by n!. For example - 4! = 4 * 3 * 2 * 1 = 24 5! = 5 * 4 * 3 * 2 * 1 = 120 Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek". In this article, we are calculating the factorial of a number using JavaScript. Here, we are using two ways to find the factorial. The first one is the iterative approach, and another one is the recursive approach. Using Iterative approachHere, we are iterating a loop over the sequence of numbers to get the factorial of a given number. Using this approach, the consumption of memory is less than the recursive implementation. But the code is lengthier than the recursive method. Let's see an example of the same. Example In this example, there is a text field that requires a number and a button, which gives us the factorial of the entered number. We have to enter a number in the given textfield to find the factorial of that number. Then we need to click the given button named Factorial to get the result. If we enter a negative number, then the program calculates the factorial of 0, which is 1. Output After the execution of the above code, the output will be - ![]() After entering the number and clicking the given button, the output will be - ![]() Now, we will see how to calculate the factorial using recursive method in JavaScript. Using Recursive approachIn this approach, we are using recursion to calculate the factorial of a number. Here, we call same function again and again to get the factorial. Using recursion, we have to code less than the iterative approach. Now, we will see an example of finding the factorial of number using recursion in JavaScript. Example Here there is a function fact(), which accepts a parameter num. It is a number for which we need to calculate the factorial. The function returns 1 when the value of num is 0. In the output we will see a text field that requires number and a button which gives us the factorial of the entered number. We have to enter a number in the given textfield to find the factorial of that number. Then we need to click the given button named as Factorial to get the result. Output After the execution of the above code, the output will be - ![]() After entering the number and clicking the given button, the output will be - ![]() |
? A radio button is an icon that is used in forms to take input from the user. It allows the users to choose one value from the group of radio buttons. Radio buttons are basically used for the single selection from multiple ones, which is...
8 min read
JavaScript is a potent scripting language that gives programmers the ability to make dynamic, interactive web sites. Finding a specific element's parent element is a frequent operation when working with the Document Object Model (DOM). Here, we'll look at a variety of JavaScript-based approaches to accomplishing...
6 min read
If you know how to set a password in a JavaScript code, it becomes easy to understand how we can toggle the visibility of the password. Here, in this section, we will learn how we can toggle the visibility of the password in JavaScript by implementing...
3 min read
A bit-wise copy of an object is known as 'Shallow Copy'. When there is an original object, a new object is created where the same values as in the original object are copied. To know about Shallow copy, one should know about the types of data...
4 min read
JavaScript is an asynchronous (non-blocking) and single-threaded programming language, meaning only one process can be run at a time. In programming languages, callback hell generally refers to an ineffective way of writing code with asynchronous calls. It is also known as the Pyramid of Doom. The callback...
12 min read
The querySelector is a JavaScript method that plays a vital role in the searching of elements. In this section, we will understand and discuss the querySelector () method, its use and also look over an example to understand the concept of the querySelector () method practically. Introducing...
5 min read
An Http cookie, also known as a web cookie, is a small piece of data that the server sends to the web browser. The browser may store the cookies and send them back to the server with other requests. The typical use of the cookies...
10 min read
? A beep sound is usually used for the notification alert in websites. There may be some other cases where you need to use a beep sound in your website. You can find several real-life examples of this beep sound, such as in grocery stores, libraries, and...
5 min read
The drawing line is an easy and simple method using the canvas function on a web page using javascript. Canvas tag uses to draw the line with different javascript properties and methods. The javascript sets the line's width, structure, display, and color. JavaScript line drawing instructions You can...
7 min read
The topic is very much important for those who are JavaScript developers or want to have a deep knowledge of the working process of JavaScript. Here, in this section, we will learn and understand the execution context of JavaScript, where we will discuss what it is,...
13 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India