Stats

112 Contributors: 4
Thursday, April 13, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook

Tail Call Optimization

Download javascript eBook

Syntax

  • only return call() either implicitly such as in arrow function or explicitly, can be a tail call statment
  • function foo(){ return bar(); } // the call to bar is a tail call
  • function foo(){ bar(); }// bar is not a tail call. The function returns undefined when no return is given
  • const foo = () => bar(); // bar() is a tail call
  • const foo = () => (poo(),bar()); // poo is not a tail call, bar is a tail call
  • const foo = () => poo() && bar(); // poo is not a tail call, bar is a tail call
  • const foo = () => bar() + 1; // bar is not a tail call as it requires context to return + 1

Remarks

TCO is also known as PTC (Proper Tail Call) as it is referred to in the ES2015 specifications.

Related Examples