[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-js2go-ep37-recursion-vs-iteration-in-javascript-and-go-all--*":3,"academy-blog-translations-pc7piveewx5urgq":84},{"data":4,"page":83,"perPage":83,"totalItems":83,"totalPages":83},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":77,"keywords":78,"locale":54,"published_at":79,"scheduled_at":13,"school_blog":75,"short_description":80,"slug":81,"status":73,"title":6,"updated":82,"updated_by":13,"views":76},"JS2GO EP.37 Recursion vs Iteration in JavaScript and Go","sclblg987654321","school_blog_translations","\u003Cp>Every programmer has faced repetitive tasks such as:\u003C\u002Fp>\u003Cul>\u003Cli>Summing up a list of numbers\u003C\u002Fli>\u003Cli>Calculating a factorial\u003C\u002Fli>\u003Cli>Or generating a Fibonacci sequence\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>There are two main techniques to handle repetition in programming:\u003C\u002Fp>\u003Cul>\u003Cli>Recursion — a function that calls itself repeatedly\u003C\u002Fli>\u003Cli>Iteration — using loops like \u003Ccode inline=\"\">for\u003C\u002Fcode> or \u003Ccode inline=\"\">while\u003C\u002Fcode> to repeat actions\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Although both methods can produce the same results, they differ in performance, memory usage, and readability.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Let’s explore how JavaScript and Go implement these two approaches 👇\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>1. Recursion — Function Calling Itself\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 JavaScript: Recursion\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">function factorial(n) {\n  if (n === 0) return 1;\n  return n * factorial(n - 1);\n}\n\nconsole.log(factorial(5)); \u002F\u002F Output: 120\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Explanation:\u003Cbr>\u003Ccode inline=\"\">factorial(5)\u003C\u002Fcode> keeps calling itself until it reaches \u003Ccode inline=\"\">factorial(0)\u003C\u002Fcode>,\u003Cbr>then returns values back up the call stack by multiplying each level.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Advantages:\u003Cbr>✅ Simple and elegant — mirrors mathematical logic\u003Cbr>✅ Works well for problems with recursive structures (e.g., tree traversal)\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Disadvantages:\u003Cbr>❌ Uses a lot of stack memory — can cause “Stack Overflow” on deep recursion\u003Cbr>❌ Slower than iteration in large-scale repetitive tasks\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 Go: Recursion\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\nfunc factorial(n int) int {\n\tif n == 0 {\n\t\treturn 1\n\t}\n\treturn n * factorial(n-1)\n}\n\nfunc main() {\n\tfmt.Println(factorial(5)) \u002F\u002F Output: 120\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Explanation:\u003Cbr>Similar to JavaScript, but Go handles the stack more efficiently.\u003Cbr>Some Go compilers even perform Tail Recursion Optimization (TRO) in certain cases.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Summary:\u003Cbr>Recursion in Go is suitable for problems that require repetitive breakdowns such as:\u003C\u002Fp>\u003Cul>\u003Cli>Tree Traversal\u003C\u002Fli>\u003Cli>Depth-First Search (DFS)\u003C\u002Fli>\u003Cli>Recursive Parsing\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>2. Iteration — Using Loops\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 JavaScript: Iteration\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">function factorialIterative(n) {\n  let result = 1;\n  for (let i = 1; i &lt;= n; i++) {\n    result *= i;\n  }\n  return result;\n}\n\nconsole.log(factorialIterative(5)); \u002F\u002F Output: 120\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Advantages:\u003Cbr>✅ Uses less memory than recursion\u003Cbr>✅ Faster for large repetitive tasks\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Disadvantages:\u003Cbr>❌ Code can be slightly longer or less elegant\u003Cbr>❌ Not ideal for problems with naturally recursive structures (e.g., trees or graphs)\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 Go: Iteration\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\nfunc factorialIterative(n int) int {\n\tresult := 1\n\tfor i := 1; i &lt;= n; i++ {\n\t\tresult *= i\n\t}\n\treturn result\n}\n\nfunc main() {\n\tfmt.Println(factorialIterative(5)) \u002F\u002F Output: 120\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Advantages:\u003Cbr>✅ High performance, ideal for production systems\u003Cbr>✅ Uses minimal memory\u003Cbr>✅ Go’s compiler optimizes loops effectively\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>3. Recursion vs Iteration — Comparison\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Feature\u003C\u002Fth>\u003Cth>Recursion\u003C\u002Fth>\u003Cth>Iteration\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Concept\u003C\u002Ftd>\u003Ctd>Function calls itself\u003C\u002Ftd>\u003Ctd>Repeats using loops\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Readability\u003C\u002Ftd>\u003Ctd>Easier for complex problems\u003C\u002Ftd>\u003Ctd>Easier for general tasks\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Performance\u003C\u002Ftd>\u003Ctd>Slightly slower\u003C\u002Ftd>\u003Ctd>Much faster\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Memory Usage\u003C\u002Ftd>\u003Ctd>Higher (stack memory)\u003C\u002Ftd>\u003Ctd>Lower\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Best For\u003C\u002Ftd>\u003Ctd>Tree, Graph, Divide &amp; Conquer\u003C\u002Ftd>\u003Ctd>Looping, Counting, Accumulation\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>4. Real-World Example — Tree Traversal (DFS)\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 JavaScript: Recursive Tree Traversal\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">const tree = {\n  value: 1,\n  children: [{ value: 2 }, { value: 3, children: [{ value: 4 }] }]\n};\n\nfunction traverse(node) {\n  console.log(node.value);\n  if (node.children) {\n    node.children.forEach(traverse);\n  }\n}\n\ntraverse(tree);\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 Go: Recursive Tree Traversal\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\ntype Node struct {\n\tValue    int\n\tChildren []*Node\n}\n\nfunc traverse(n *Node) {\n\tfmt.Println(n.Value)\n\tfor _, child := range n.Children {\n\t\ttraverse(child)\n\t}\n}\n\nfunc main() {\n\ttree := &amp;Node{\n\t\tValue: 1,\n\t\tChildren: []*Node{\n\t\t\t{Value: 2},\n\t\t\t{Value: 3, Children: []*Node{{Value: 4}}},\n\t\t},\n\t}\n\ttraverse(tree)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Output:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">1\n2\n3\n4\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>5. Choosing the Right Approach\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>🧠 Use Recursion Best for problems with hierarchical or nested structures, such as:\u003C\u002Fp>\u003Cul>\u003Cli>Tree \u002F Graph Traversal\u003C\u002Fli>\u003Cli>Divide &amp; Conquer algorithms (e.g., Merge Sort, Quick Sort)\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>⚙️ Use Iteration Best for common tasks such as:\u003C\u002Fp>\u003Cul>\u003Cli>Looping, Counting, or Summation\u003Cbr>Especially in production systems that require high performance and stability.\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>💡 In Go: Loops are generally better optimized than recursion.\u003Cbr>💻 In JavaScript: Recursion is great for learning and conceptual examples,\u003Cbr>but iteration tends to be more stable in production environments.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Conclusion\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Both Recursion and Iteration are essential programming tools. Choosing the right one can make your code cleaner, faster, and more memory-efficient.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Scenario\u003C\u002Fth>\u003Cth>Recommended Approach\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Tree \u002F Graph Problems\u003C\u002Ftd>\u003Ctd>🧠 Recursion\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>General Loops \u002F Counting Tasks\u003C\u002Ftd>\u003Ctd>⚙️ Iteration\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>High Performance Systems\u003C\u002Ftd>\u003Ctd>⚡ Iteration (especially in Go)\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Easy-to-Understand Code\u003C\u002Ftd>\u003Ctd>💡 Recursion\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Next Episode\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>In EP.38 of JS2GO,\u003Cbr>we’ll dive into Buffer and Stream Management in Node.js and Go\u003Cbr>understanding how to handle large-scale data efficiently, from reading files and network transmissions to managing memory in production systems ⚙️\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp data-start=\"498\" data-end=\"834\">\u003Cstrong>Read more\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FGolang\">\u003Cstrong>Golang The Series\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FJS2GO\">\u003Cstrong>JS2GO\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fen\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\">\u003Cstrong>10 Eps That Will Make You a Pro Tailwind CSS Overnight\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🔴 YouTube: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.youtube.com\u002Fchannel\u002FUC2eI2RxcA2zbTqZyHyWIfRg\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevschool\u002F\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdevacademy.com\u002Fen\">\u003Cstrong>https:\u002F\u002Fwww.superdevacademy.com\u002Fen\u003C\u002Fstrong>\u003C\u002Fa>\u003Cstrong>&nbsp;\u003C\u002Fstrong>\u003C\u002Fp>","74_11zon_hmgjcso3mr.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F2gl0tq249xm0c9l\u002F74_11zon_hmgjcso3mr.webp","2026-03-04 08:45:32.281Z","",{"keywords":15,"locale":48,"school_blog":58},[16,23,28,33,38,43],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:45:30.765Z","nayuhpre9emd5xw","JS2GO","2026-04-10 16:12:59.160Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:20:42.484Z","vslzz9nvv6n77cx","JavaScript","2026-04-10 16:07:28.339Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:45:31.134Z","erv835t5leo90qz","Iteration","2026-04-10 16:12:59.301Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:45:31.539Z","thoynmyxpcmdicf","Recursion","2026-04-10 16:12:59.439Z",{"code":49,"collectionId":50,"collectionName":51,"created":52,"flag":53,"id":54,"is_default":55,"label":56,"updated":57},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":59,"collectionId":60,"collectionName":61,"expand":62,"id":75,"views":76},"hsa1afr8fcnd6qb","pbc_2105096300","school_blogs",{"category":63},{"blogIds":64,"collectionId":65,"collectionName":66,"created":67,"created_by":13,"id":59,"image":68,"image_alt":13,"image_path":69,"label":70,"name":21,"priority":71,"publish_at":72,"scheduled_at":13,"status":73,"updated":74,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:24:37.986Z","js2_go_2_11zon_y6paxmuz32.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fhsa1afr8fcnd6qb\u002Fjs2_go_2_11zon_y6paxmuz32.webp",{"en":21,"th":21},10,"2025-08-11 03:41:08.820Z","published","2026-04-25 02:32:14.339Z","pc7piveewx5urgq",207,"2gl0tq249xm0c9l",[20,25,30,35,40,45],"2025-11-12 02:36:47.705Z","Explore the key differences between Recursion (self-calling functions) and Iteration (loops) with practical examples in JavaScript and Go. Learn which approach offers better performance, readability, and memory efficiency for real-world systems.","js2go-ep37-recursion-vs-iteration-in-javascript-and-go","2026-04-25 02:47:54.140Z",1,{"en":81}]