I'd avoid using Array.prototype.every in this way.
It too will exit as soon as an entry returns false. MDN - array.prototype.every
It's not because of .every that all of the validations run.
It's because the array is created and all of its items are evaluated, before.every executes
You would get the same result with
functionsaveProfileIfValid(){constisValid={firstName:firstNameIsValid('Jerry'),// <- executes before if( ... )lastName:lastNameIsValid('Smith'),// <- executes before if( ... )email:emailIsValid('js@email.com')// <- executes before if( ... )}// all the validations have already happenedif(isValid.firstName&&isValid.lastName&&isValid.email){saveProfile();// save only if all validations pass}}
I am still relatively new to Javascript so I appreciate this (I guess Stack Overflow doesn't always have the best solution haha). I will update my code. Thank you both!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'd avoid using
Array.prototype.every
in this way.It too will exit as soon as an entry returns false.
MDN - array.prototype.every
It's not because of
.every
that all of the validations run.It's because the array is created and all of its items are evaluated, before
.every
executesYou would get the same result with
Hope this helps
I thought the same! Good catch. All array methods that rely on the iteratee to return a boolean will stop execution as soon as possible.
I am still relatively new to Javascript so I appreciate this (I guess Stack Overflow doesn't always have the best solution haha). I will update my code. Thank you both!