recursion in javascript code example

Example 1: recursive function javascript

function pow(x, n) {
  if (n == 1) {
    return x;
  } else {
    return x * pow(x, n - 1);
  }
}

alert( pow(3, 3) ); // 27

Example 2: recursion

/*Java*/
static void recursion(){ recursion(0); }
static void recursion(int x){
	System.out.println("eheh " + x);
    if(x != 666) recursion(x+1);
}

Example 3: javascript recursion over array

function recurse(arr=[])
{
  	// base case, to break the recursion when the array is empty
    if (arr.length === 0) {
    	return;
    }
  	
  	// destructure the array
	const [x, ...y] = arr;
  
  	// Do something to x
  	
  	return recurse(y);
}

Example 4: recursion in javascript

var countdown = function(value) {
    if (value > 0) {
        console.log(value);
        return countdown(value - 1);
    } else {
        return value;
    }
};
countdown(10);