Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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?

+0
−4

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?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

There are no unnamed variables... (7 comments)
There are no unnamed variables...
elgonzo‭ wrote almost 2 years ago · edited almost 2 years ago

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? ;-)

deleted user wrote almost 2 years ago

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.

hkotsubo‭ wrote almost 2 years ago

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

elgonzo‭ wrote almost 2 years ago · edited almost 2 years ago

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)

deleted user wrote almost 2 years ago

hkotsubo‭

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.

hkotsubo‭ wrote almost 2 years ago · edited almost 2 years ago

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 :-)

deleted user wrote almost 2 years ago

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.