DEV Community

pookdeveloper
pookdeveloper

Posted on

1

How to break out of the forEach in Javascript/NodeJS

It’s simple.

Use “some” or “every” instead of “forEach”

Use of Array.prototype.some

The some() method checks if at least one element of the array meets the condition implemented by the function provided.

Example:

The result should look like:

1
2
"Result:" true

More info: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/some


Use of Array.prototype.every

The every() method returns a Boolean, true if all the elements in the array pass the condition implemented by the given function and false if one does not fulfill it.

Example:

The result should look like:

1
2
“Result:” false

More info: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/every

That’s all folks

JavaScript tools that help you ship faster

JavaScript tools that help you ship faster

Skip boilerplate and focus on features. Kinde handles auth, access control, and billing behind the scenes.

Get a free account

Top comments (0)

JavaScript-ready auth and billing that just works

JavaScript-ready auth and billing that just works

Stop building auth from scratch. Kinde handles authentication, user management, and billing so you can focus on what matters - shipping great products your users will love.

Get a free account

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay