I was wondering if someone could point the spotlight on some weird behavior I just found. So, I was working in the node REPL, running code before I stick it in a module, and I made a typo (well, didn't completely fill the line in) of 'let = 5'. I expected this to error out, but the REPL accepted it, and I can see the value with expression results and console.logs. So I started tinkering. I know that in the REPL, variables without let/const/var are considered global, but what I was wondering is, why does the REPL allow us to assign let like this? Below I have listed the things I've tried (in REPL only, have not tried in module script).
let = 5; //works
var = 5; //SyntaxError: Unexpected token "="
const = 5; //SyntaxError: Unexpected token "="
let let = 5; //SyntaxError: let is disallowed as a lexically bound name
var let = 5; //works
const let = 5; //SyntaxError: let is disallowed as a lexically bound name
let var = 5; //SyntaxError: Unexpected token "var"
var var = 5; //SyntaxError: Unexpected token "var"
const var = 5; //SyntaxError: Unexpected token "var"
let const = 5; //SyntaxError: Unexpected token "const"
var const = 5; //SyntaxError: Unexpected token "const"
const const = 5; //SyntaxError: Unexpected token "const"
So why does let = 5 and var let = 5 work when logically (to me at least), all of those statements should have been syntax errors?
Edit To add, let still works the same when used to assign a variable after being assigned. E.g.
let = 5;
let test = {};
console.log(let,test)
works, and displays 5 {}