Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Does a for...of loop must contain a variable without assignment sign, and why?
Post
Does a for...of loop must contain a variable without assignment sign, and why?
The following code pattern reflects this answer.
This code contains a for...of
loop:
for (const child of e.childNodes) {
// Do stuff;
}
Does a for...of
loop must contain a variable without assignment sign (=
), and why?
Is there a way to achieve the same result with "regular" variable declaration?
Your code snippet features only two variables, both are named. One is named "e", the other is named "child". Variables by definition always have a name (more specifically: an identifier). There are no unnamed variables (unless one would consider an indexed storage location/position in a collection or set as a variable).
Assuming unnamed variables were possible, the name of an unnamed variable would be "" (empty string). They would be practically invisible in the source code. Now, wouldn't it be fun if we could sprinkle invisible variables all around our source codes? ;-)
elgonzo what mislead me is the lack on =
right after const
.
I am just not used to declare variables this way and never had any need of using for...of
before.
There's no =
after const
in any variable declaration. You either declare it as const variableName;
or const variableName = someValue;
(the =
is after the variable name, not after const
- yes, that was pedantic from my part, sorry)
Anyway, this for..of
code is equivalent to:
for (var i = 0; i < e.childNodes.length; i++) {
const child = e.childNodes[i];
// do stuff
}
The for..of
just have a different syntax, as the variable is declared inside the parenthesis. But the variable is not unnamed
Additionally to what @hkotsubo wrote, =
(assignment) would not be entirely correct here. (It would have been possible to design the language to allow =
, but it would make this operator essentially somewhat ambiguous in its use.)
for (const e of {some enumeration} )
is iterating over an enumeration of values/objects, with each iteration the next value/object being assigned to "e".
However, a syntax like for (const e = {some enumeration} )
lends itself to the suggestion that the enumeration itself is being assigned to "e". Which is of course not what the for
statement does, so it is better for both readability and reduced risks of making coding errors to use a different operator keyword of
instead of overloading the =
operator with similar yet different semantics/meanings. (A pet peeve of mine: languages with an ==
operator which also allow assignment expressions, which is always good for a lot of fun when making a typo wrt to =
vs. ==
in an expression)
I find the for
syntax less confusing than for...of
syntax, which for me kind of breaks the "regular" flow of almost any elementary/basic JavaScript code I ever worked with.
The purpose of for..of
is not only to provide a different syntax. With this statement, you can loop through any iterable object (you can even create your own). Some objects, for instance, don't have the length
property or don't support the [ ]
syntax to get a specific element, so using for..of
is a much straightforward (and standard) way to traverse them. Check the docs for more use cases.
But anyway, that's beside the point. The answer to your question is "yes" - that's the syntax of for..of
statement, so the variable must not have the assignment operator. Regarding "why?", it's because the people who defined it decided it that way :-)
elgonzo through the years I wrote a few JavaScript codes which just work so It's not that I don't understand all JavaScript fundamentals, rather, I don't understand some or most as user @r~~ pointed out and I agree with him. I should learn everything I don't know. I wish I should reply to you differently at start.
I have already asked the mods to delete my account.
This community is part of the Codidact network. We have other communities too — take a look!
You can also join us in chat!
Want to advertise this community? Use our templates!
Like what we're doing? Support us! Donate
1 comment thread