, 10 tweets, 12 min read
My Authors
Read all threads
🔥🔥🔥 The #1 Recursion (Syntax) Twitter Thread

This thread will demonstrate an easy way to write recursive functions

The recursion will so be compared to a for loop for familiarity

Tail Calls and Performance discussed later, Syntax Only!

gist/theme at end 👇

#javascript
Step 1: Create your iteration variable(s)

In recursion, it can be easier to set a default value. Pass `i` again when calling `loop` on the `return` line

#javascript #code #dev #es6
Step 2: Add a break condition

Like a for loop, add the break before you write your code. This will prevent infinite looping.

Notice how the for loop is a "continue" and the recursion uses a "break". That means the condition must be flipped for recursion

#javascript #nodejs
Step 3: Increment the variable(s) `i`

Like the for loop, this is considered the base for recursion. This should be completed before writing your code.

Instead of `i++` (mutation), the increment happens in the `return loop(i + 1)`.

#javascript #nodejs #es6
Step 4: Inputs

A for loops inputs are available in the scope. A recursions inputs should be function arguments.

These "input" function arguments should be added to the left of the `i`

Remember to pass them in the `return`

#javascript #dev #nodejs #es6
Step 5: Outputs

A for loop's output will mutate a variable in the scope

A recursive function will return a new value

The output should be added between the inputs and `i`

Be sure to add `output` to both `return` statements!

#javascript #nodejs #es6 #dev #code #development
Bonus: Getting Fancy

Functions can be expressed in many ways. This code shows the function written as a function expression.

The code is using `concat`, which is fine for small arrays and concise code, but won't work well for large arrays.

#javascript #nodejs #dev #es6 #code
Bonus: Going Fast

When concise code AND performance are important, consider using an immutable library

This will let you write immutable code in a way that is also has performance

#javascript #nodejs #code #dev #es6 #development
Code Pen: codepen.io/joelnet/pen/OJ…

Editor: VSCode
Font: FiraCode (with ligatures)
Theme: marketplace.visualstudio.com/items?itemName…

Cheers!🍻🍻🍻

#javascript #es6 #nodejs #code #dev #development #programming
P.S. If you found this useful or fun. Retweet, Forward to a friend/coworker and subscribe for MORE!

Cheers! 🍻
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with JavaScript Joel 🍻

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!