<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/refactoring-nested-loops-in-js-6o5fot3d9" -->

---
title: Refactoring Nested Loops In JS | daily.dev
description: Most of the code in JS gets nested because of the various conditions one has to handle for the solution. Ternary operator is the best and most common way to...
canonical: https://daily.dev/posts/refactoring-nested-loops-in-js-6o5fot3d9
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Refactoring Nested Loops In JS | daily.dev
og:description: Most of the code in JS gets nested because of the various conditions one has to handle for the solution. Ternary operator is the best and most common way to...
og:url: https://daily.dev/posts/refactoring-nested-loops-in-js-6o5fot3d9
og:image: https://api.daily.dev/og/posts/6o5fOt3D9.png
og:image:alt: Refactoring Nested Loops In JS
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Refactoring Nested Loops In JS

**[Towards Dev](https://daily.dev/sources/towardsdev)** · 3 min read · 124 upvotes · 4 comments

## Summary

Most of the code in JS gets nested because of the various conditions one has to handle for the solution. Ternary operator is the best and most common way to replace your “if…else” statements. Switch case statement is not the best possible way to. replace nested ‘if-else’ statements but these can be used to reduce. duplicate code which is another common mistake ignored by developers.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://towardsdev.com/refactoring-nested-loops-in-js-e6d8a9db2db3>

## Community discussion

Top comments from developers on daily.dev.

**@dudousxd** · 5 upvotes

> The title is "Refactoring Nested Loops In JS" but when you go to the article it's "Refactoring If...Else Statement In JS", what happened here?

**@corem96** · 2 upvotes

> great refactoring alternatives but also very useful ways to write better code

**@maxalthaus** · 1 upvotes

> I agree and i like the ternary operater

## Similar posts on daily.dev

- [Don’t just attend KubeCon \+ CloudNativeCon, Merge Forward your experience\!](https://daily.dev/posts/don-t-just-attend-kubecon-cloudnativecon-merge-forward-your-experience--l0rpp73x8) · CNCF · 1 upvotes · 0 comments
- [Announcing H2 2026 KCDs](https://daily.dev/posts/announcing-h2-2026-kcds-m96goajm1) · CNCF · 1 upvotes · 0 comments
- [Two months of Open Community Groups](https://daily.dev/posts/two-months-of-open-community-groups-asf52zhbs) · CNCF · 0 upvotes · 0 comments
- [CNCF Unveils Schedule for KubeCon \+ CloudNativeCon Europe 2026](https://daily.dev/posts/cncf-unveils-schedule-for-kubecon-cloudnativecon-europe-2026-ikhcoa5cb) · CNCF · 2 upvotes · 0 comments
- [CNCF Debuts KubeCon \+ CloudNativeCon Japan 2026 Schedule](https://daily.dev/posts/cncf-debuts-kubecon-cloudnativecon-japan-2026-schedule-xp5pyudub) · CNCF · 1 upvotes · 0 comments

---

Tags: [#general-programming](https://daily.dev/tags/general-programming), [#javascript](https://daily.dev/tags/javascript)

[View this post on daily.dev](https://daily.dev/posts/refactoring-nested-loops-in-js-6o5fot3d9)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Refactoring Nested Loops In JS","url":"https://daily.dev/posts/refactoring-nested-loops-in-js-6o5fot3d9","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/refactoring-nested-loops-in-js-6o5fot3d9"},"datePublished":"2021-12-09T06:58:13.489Z","dateModified":"2021-12-09T06:58:13.489Z","description":"Most of the code in JS gets nested because of the various conditions one has to handle for the solution. Ternary operator is the best and most common way to...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b994c03976f7e608e0133296b9bbb20f","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b994c03976f7e608e0133296b9bbb20f","isAccessibleForFree":true,"articleSection":"Towards Dev","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Towards Dev","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/2c710db2750a4a70835862534cb7c9d4","url":"https://daily.dev/sources/towardsdev"},"commentCount":4,"discussionUrl":"https://daily.dev/posts/refactoring-nested-loops-in-js-6o5fot3d9","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":124},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":4}],"keywords":"general-programming,javascript","timeRequired":"PT3M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Towards Dev","item":"https://daily.dev/sources/towardsdev"},{"@type":"ListItem","position":3,"name":"Refactoring Nested Loops In JS"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/refactoring-nested-loops-in-js-6o5fot3d9","comment":[{"@type":"Comment","text":"The title is “Refactoring Nested Loops In JS” but when you go to the article it’s “Refactoring If…Else Statement In JS”, what happened here?","datePublished":"2021-12-09T17:48:20.267Z","url":"https://daily.dev/posts/6o5fOt3D9#c-1rYXWwl-z","author":{"@type":"Person","name":"Davi de Carvalho","url":"https://daily.dev/dudousxd","image":"https://avatars1.githubusercontent.com/u/15630886?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":5}},{"@type":"Comment","text":"great refactoring alternatives but also very useful ways to write better code","datePublished":"2021-12-09T17:00:09.146Z","url":"https://daily.dev/posts/6o5fOt3D9#c-XRR7ryO83","author":{"@type":"Person","name":"Jorge Escamilla Mendoza","url":"https://daily.dev/corem96","image":"https://avatars.githubusercontent.com/u/18492412?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}},{"@type":"Comment","text":"I agree and i like the ternary operater","datePublished":"2022-01-14T18:27:55.689Z","url":"https://daily.dev/posts/6o5fOt3D9#c-8XUCPMC_j","author":{"@type":"Person","name":"Balthasar-Maximilian Althaus","url":"https://daily.dev/maxalthaus","image":"https://media.daily.dev/image/upload/s--FBOoVcn---/f_auto/v1748496727/avatars/avatar_UNDRxv5P00vGmMsfbPGBI?_a=BAMClqUq0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}}]}
```

