I accidentally wrote a weird declaration in my code
let = x = 5;
And it took me some time to actually notice it as it worked as expected (x was indeed 5). I wanted to ask why is that? Is it interpreted similarly as
let y = x = 5;
(just with the missing second variable) or has it some other function? It bothered me since and I can't seem to find an answer
CodePudding user response:
You assigned the value of 5 to both let
and x
:
let = x = 5;
console.log(let);
// Outputs 5
However, this doesn't interfere with the let
keyword, and it still works as per usual.
CodePudding user response:
This is for backwards compatibility.
let
was a valid variable name before it was added to the JavaScript language, and needs to remain so to avoid breaking old software.
let = 5;
console.log(let);
This isn't the case for code running in strict mode (which I strongly advise using if at all possible).
<script type="module">
let = 5;
console.log(let);
</script>