Why is regex.exec() return type is a boolean?

javascript, regex, return-type

Solution

Because `arr` isn't the result of `exec`, it's the result of `!==` (which should be either `true` or `false`).

In other words, `x = y !== z` parses as `x = (y !== z)`, not `(x = y) !== z`.

You probably meant to write

while ((arr = pattern.exec(str)) !== null) {

instead.

Problem

I'm quite new to javascript and have an issue on regex According to this documentation page, the regex.exec() function should return either an array or null if there's no match. if the match succeeds, the exec() method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured. If the match fails, the exec() method returns null. why then in my code, the result of exec() is either a boolean or null? ``` function matchHTMLsymbols(str) var pattern = /&|<|>|"|' /g; var arr; while ((arr = pattern.exec(str) !== null)) { console.log(arr); } } ```

Original source