With additions to the language it is much easier, plus you can take advantage of early return:
// Use includes method on string
function hasWhiteSpace(s) {
const whitespaceChars = [' ', '\t', '\n'];
return whitespaceChars.some(char => s.includes(char));
}
// Use character comparison
function hasWhiteSpace(s) {
const whitespaceChars = [' ', '\t', '\n'];
return Array.from(s).some(char => whitespaceChars.includes(char));
}
const withSpace = "Hello World!";
const noSpace = "HelloWorld!";
console.log(hasWhiteSpace(withSpace));
console.log(hasWhiteSpace(noSpace));
console.log(hasWhiteSpace2(withSpace));
console.log(hasWhiteSpace2(noSpace));
I did not run performance benchmark but these should be faster than regex but for small text snippets there won't be that much difference anyway.