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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more