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

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay