I stumbled upon unexpected behaviour in JS. If I log regexpr.test(inp)
, console outputs true
, yet if this code is used as condition 'if block' console.log('in if');
is not executed.
If I assign this code to a variable and then use it as a condition 'if block' gets executed as expected.
Tested it in chrome console and node.
var str = '()'
function validateInput (inp) {
let regexpr = /^(?:\(|\)|\s)+$/gim
console.log(regexpr.test(inp)); // logs true
if (regexpr.test(inp)) { // staments in if are not executed
console.log('in if');
}
console.log('after if');
return false
}
function validateInput2 (inp) {
let regexpr = /^(?:\(|\)|\s)+$/gim
let t1 = regexpr.test(inp);
console.log(t1); // logs true
if (t1) { // staments in if are executed
console.log('in if');
}
console.log('after if');
return false
}
console.log(validateInput(str));
console.log('------')
console.log(validateInput2(str));
true
after if
false
------
true
in if
after if
false