When using RegExp.prototype.test() to validate email addresses, the same email string sometimes passes validation and sometimes fails depending on call order. Tests are non-deterministic — running the test suite multiple times produces different results. A function called isValidEmail() uses a global regex /^[^@]+@[^@]+\.[^@]+$/g to validate emails, and calling it multiple times in succession causes alternating true/false results for the same input.
The /g (global) flag on a regex causes the RegExp object to maintain a lastIndex property that persists between calls to .test(). After a successful match, lastIndex advances past the matched text. The next .test() call begins searching from that position instead of the string start, causing it to fail on full-string patterns. The lastIndex then resets to 0, so the following call succeeds again — creating the alternating pattern. The fix is to remove the /g flag: const EMAIL_RE = /^[^@]+@[^@]+\.[^@]+$/;. The /g flag is only needed for String.prototype.matchAll(), replaceAll(), or iterating multiple matches with exec(). For validation patterns using ^...$ anchors and .test(), it serves no purpose and introduces this stateful bug.