console.log("#1", "a12312a".match(/^\d+/)?.[0].length);
console.log("#2", ("a12312a".match(/^\d+/)?.[0]).length);
I was writing some code and stumbled upon something I don’t understand. In Chrome 89.0.4389.128 (Official Build) (64-bit), the code above gives this:
#1 undefined
Uncaught TypeError: Cannot read property 'length' of undefined
The both lines look the same to me: "a12312a".match(/^\d+/)?.[0] is an undefined, and they are trying to read the property length of the undefined, which is supposed to throw a TypeError. But the first line did not, while the second did.
…Why? I’m confused. Am I missing something very basic?