If you gaze long into an abyss, the abyss also gazes into you, avoiding the empty void in JS

Recently, I came across an interesting scenario while working with list checks in JavaScript. Take a look at the following code:

appointment.serviceRequests?.every(
serviceRequest => serviceRequest.createdMethod === ServiceRequestMethods.INTEGR…


This content originally appeared on DEV Community and was authored by Marcos Henrique

Recently, I came across an interesting scenario while working with list checks in JavaScript. Take a look at the following code:

appointment.serviceRequests?.every(
  serviceRequest => serviceRequest.createdMethod === ServiceRequestMethods.INTEGRATED,
);

At first glance, it seems like we're checking if all serviceRequests in an appointment were created using a specific method (INTEGRATED). However, there's a subtle issue here: what if serviceRequests is an empty list? 🤔

What happens?

In JavaScript, the .every() method returns true for empty lists. This happens because, when there are no elements to check, the statement is considered "vacuously true" (a concept from mathematical logic). In other words, since there are no elements to contradict the condition, the result is true.

This can lead to unexpected behaviour. For example, if serviceRequests is [] (an empty list), the code above will return true, even though there are no serviceRequests to validate.

How to fix it?
To avoid this behaviour, we can add a check to ensure the list is not empty before using .every(). Here's the solution:

appointment?.serviceRequests?.length &&
appointment.serviceRequests?.every(
  serviceRequest => serviceRequest.createdMethod === ServiceRequestMethods.INTEGRATED,
);

Here, we:

Check if serviceRequests exists and is not empty (appointment?.serviceRequests?.length).

Apply .every() only if the list is not empty.

Why does this matter?

Understanding these details is crucial for writing robust code and avoiding subtle bugs. The concept of "vacuously true" can be useful in many contexts, but it can also confuse if not handled properly.

đź’ˇ Tip: Always consider edge cases, such as empty lists, when writing conditional checks.

Have you encountered something similar? Share your experiences in the comments! 👇


This content originally appeared on DEV Community and was authored by Marcos Henrique


Print Share Comment Cite Upload Translate Updates
APA

Marcos Henrique | Sciencx (2025-03-21T16:30:18+00:00) If you gaze long into an abyss, the abyss also gazes into you, avoiding the empty void in JS. Retrieved from https://www.scien.cx/2025/03/21/if-you-gaze-long-into-an-abyss-the-abyss-also-gazes-into-you-avoiding-the-empty-void-in-js/

MLA
" » If you gaze long into an abyss, the abyss also gazes into you, avoiding the empty void in JS." Marcos Henrique | Sciencx - Friday March 21, 2025, https://www.scien.cx/2025/03/21/if-you-gaze-long-into-an-abyss-the-abyss-also-gazes-into-you-avoiding-the-empty-void-in-js/
HARVARD
Marcos Henrique | Sciencx Friday March 21, 2025 » If you gaze long into an abyss, the abyss also gazes into you, avoiding the empty void in JS., viewed ,<https://www.scien.cx/2025/03/21/if-you-gaze-long-into-an-abyss-the-abyss-also-gazes-into-you-avoiding-the-empty-void-in-js/>
VANCOUVER
Marcos Henrique | Sciencx - » If you gaze long into an abyss, the abyss also gazes into you, avoiding the empty void in JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/21/if-you-gaze-long-into-an-abyss-the-abyss-also-gazes-into-you-avoiding-the-empty-void-in-js/
CHICAGO
" » If you gaze long into an abyss, the abyss also gazes into you, avoiding the empty void in JS." Marcos Henrique | Sciencx - Accessed . https://www.scien.cx/2025/03/21/if-you-gaze-long-into-an-abyss-the-abyss-also-gazes-into-you-avoiding-the-empty-void-in-js/
IEEE
" » If you gaze long into an abyss, the abyss also gazes into you, avoiding the empty void in JS." Marcos Henrique | Sciencx [Online]. Available: https://www.scien.cx/2025/03/21/if-you-gaze-long-into-an-abyss-the-abyss-also-gazes-into-you-avoiding-the-empty-void-in-js/. [Accessed: ]
rf:citation
» If you gaze long into an abyss, the abyss also gazes into you, avoiding the empty void in JS | Marcos Henrique | Sciencx | https://www.scien.cx/2025/03/21/if-you-gaze-long-into-an-abyss-the-abyss-also-gazes-into-you-avoiding-the-empty-void-in-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.