Edit: a - lookalike character was the culprit.
I have a script I'm pasting into Edge's developer console that breaks with this:
let args;
if (typeof arguments === 'undefined') {
args = [1, 'January', 2023, 'test', x => console.log(x)];
} else {
args = arguments;
}
let returnCallback = args[args.length - 1];
The last line is what triggers "Uncaught SyntaxError: invalid or unexpected token".
I can replace args.length - 1
with 4
, and the code runs how I expect it to.
I can even replace args.length - 1
with args.length
, and no errors are thrown.
I tried pasting all but the last line in, then pressing enter. I confirmed args
is defined. Pasting that last line in still throws the error.
This is the minimum code I can reduce it to and still throw the error*:
let x = [1, 2];
x.length - 1;
The following does not throw the error:
let x = [1, 2];
x.length;
When I enter the lines individually, it gets tricky.
First, I will enter let x = [1, 2];
and press enter.
If I paste x.length - 1;
in and hit enter, I get the error. But if I type x.length - 1;
, it will not give the error.
(If I set x to a single-element array, it will still error when pasting the 2 lines together. But pasting them individually always works. It needs 2+ elements for pasting vs typing to matter.)
I don't think it is just chrome cashing x.length
when it previews the value as you type it.
For example, look at the result of these 3 lines, all pasted:
let x = [1, 2];
enter
Result: undefined
x.length
enter
Result: 2
x.length - 1
enter
Uncaught SyntaxError: Invalid or expected token
The error it gives isn't what you'd expect in that scenario either.
If I run x.length - 1
without declaring x, the output is "Uncaught ReferenceError: x is not defined"
If I run it after let x;
, the output is "Uncaught TypeError: Cannot read properties of undefined"
If I set x to something without a length property, the result is NaN
So it's more than the console having an incorrect or no value for the variable.
Is this a problem with my code? Or am I running into a bug on Edge?
Turns out I was using the wrong "-" sign. I was using "−" (U-2212 "Minus Sign") Thanks to Sebastian Simon for suggesting I check for this.