Why this code works? (bizarre syntax in javascript) [duplicate] - javascript

This question already has answers here:
Using 'let' as a variable name is not throwing any errors in google v8
(3 answers)
Closed 5 years ago.
I found this line of code works:
for(let in [0,1,2]) {console.log('wtfjs');}
but not these:
for(var in [0,1,2]) {console.log('js');}
for(const in [0,1,2]) {console.log('js');}
(Try those on Chrome, Firefox even Edge!)
I'm totally puzzled with it.
Found Another one
for(let of [0,1,2]) {console.log(let);}
Why does this not work?

It appears that let implies the variable name 'let' where var and const do not imply a name. You need a variable or const name in the ones that don't work. Like this:
for(var x in [0,1,2]) {console.log('js');}
for(const y in [0,1,2]) {console.log('js');}

Related

'const' scope in for loop JavaScript [duplicate]

This question already has answers here:
Explanation of `let` and block scoping with for loops
(5 answers)
Re-assign/declare a const variable in a JavaScript for-loop and save it more than one time with different values?
(2 answers)
const variable can be redeclared inside while loop even though it should be constant
(1 answer)
Closed last year.
In the code below, is ele available only for an individual iteration of the loop?
for (var i=0; i<3; i++){
const ele = i
console.log(ele)
}
I'm thinking this should be the case, otherwise there will be a
Syntax Error: Identifier 'ele' has already been declared
every time the loop iterates after the very first time.
I could not find a way to confirm this using console.log(), so looking for a concrete answer.
EDIT: To clarify, I understand that ele is valid within the for loop only. My question is specifically if the scope is ' reinstantiated' every iteration of this loop or not. It definitely looks like it, but I haven't heard or read that explicitly anywhere yet.
why do not directly use console.log(i) ?
howei think you should try to use var or let instead of const

What does it mean by a variable declared by let can't be redeclared in javascript? [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed last year.
When I search for difference between let and var in javascript it says variable declared by let can't be redeclared but variables declared by var can be redeclared but does this line mean?
Well hello Vinay Singla the thing is when you use let you do not have to redeclare it.
Otherwise an error will pop and a new let is very similar when you declare variables in python .
Code you wrote will cause an error

How to add number with variable name? [duplicate]

This question already has answers here:
How do I create dynamic variable names inside a loop?
(8 answers)
Closed 3 years ago.
Hi I want to change the variable name like stop1, stop2, stop3 etc in a loop.
I tried using for loop with stop + i but it didnt work
Please help
var varMap = {};
for(var i=1; i<10; i++) varMap['stop'+i] = 123;
I think this will help

Why can’t name the variable const, but in objects can [duplicate]

This question already has answers here:
Using reserved words as property names, revisited
(4 answers)
Closed 3 years ago.
I wrote.
let const = 10;
and got error.and for objects everything works
let x = {const:10}
What is the difference.
Variables can be used in the same places as you can use the const keyword. If you could use const as a variable name it would be hard to distinguish between the two.
That is not the case for property names; the syntax will always be clear that it refers to a property name.
const is a reserved keyword you cannot use as variable name, so the first one is invalid syntax,
let const = 10
In second example you're using const as key which can be any string
let x = {const:10}
console.log(x)

How it's possible to declare a variable named let in JavaScript? [duplicate]

This question already has answers here:
Using 'let' as a variable name is not throwing any errors in google v8
(3 answers)
Closed 4 years ago.
I'm wondering how a variable can be named let because let is a reserved word in JavaScript!
var let = "a value"; // this works!!!
let let = "a value"; // luckily this doesn't work
While reading about reserved words in JavaScript I used this website and also I checked this Stackoverflow question (see the answer of art4theSould) and both of them (also the same thing in other sources) said that let -as everyone can image- is a reserved word in JavaScript.
According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords, let is only reserved when it's found in strict mode code.
This appears to be for backward compatibility, so that pre-ES6 code is not impacted. This explains why let let is not allowed: since you're actually using the new let keyword, pre-ES6 compatibility is obviously not a concern.

Categories