Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

recursive function javascript

/*
Recursion is when a function calls itself until someone stops it. 
If no one stops it then it'll recurse (call itself) forever.
*/

// program to count down numbers to 1
function countDown(number) {
    // display the number
    console.log(number);
    // decrease the number value
    const newNumber = number - 1;
    // base case
    if (newNumber > 0) {
        countDown(newNumber);
    }
}

countDown(4);
// Out put: 
4
3
2
1
Source by www.javascripttutorial.net #
 
PREVIOUS NEXT
Tagged: #recursive #function #javascript
ADD COMMENT
Topic
Name
7+8 =