Is is possible to create a for-of loop without a variable?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
Suppose I have a function like this:
const hasAny = xs =>
for (const x of xs)
return true;
return false;
;
eslint will complain that x
is not used, and indeed it isn't.
Can I write a for-of loop that does not declare any variables?
javascript for-loop
 |Â
show 1 more comment
up vote
6
down vote
favorite
Suppose I have a function like this:
const hasAny = xs =>
for (const x of xs)
return true;
return false;
;
eslint will complain that x
is not used, and indeed it isn't.
Can I write a for-of loop that does not declare any variables?
javascript for-loop
2
Maybe for what you need with anreturn xs.length > 0
is enough for you.
– Jesús Magallón
15 mins ago
2
@JesúsMagallón - Not all iterables have alength
property.
– T.J. Crowder
15 mins ago
Would eslint be happy if you named the variable_
?
– Bergi
10 mins ago
@Bergi sadly this isn't ML
– sdgfsdh
7 mins ago
@sdgfsdh looks like"varsIgnorePattern": "^_"
should be the default eslint configuration
– Bergi
5 mins ago
 |Â
show 1 more comment
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Suppose I have a function like this:
const hasAny = xs =>
for (const x of xs)
return true;
return false;
;
eslint will complain that x
is not used, and indeed it isn't.
Can I write a for-of loop that does not declare any variables?
javascript for-loop
Suppose I have a function like this:
const hasAny = xs =>
for (const x of xs)
return true;
return false;
;
eslint will complain that x
is not used, and indeed it isn't.
Can I write a for-of loop that does not declare any variables?
javascript for-loop
javascript for-loop
asked 19 mins ago


sdgfsdh
7,18863581
7,18863581
2
Maybe for what you need with anreturn xs.length > 0
is enough for you.
– Jesús Magallón
15 mins ago
2
@JesúsMagallón - Not all iterables have alength
property.
– T.J. Crowder
15 mins ago
Would eslint be happy if you named the variable_
?
– Bergi
10 mins ago
@Bergi sadly this isn't ML
– sdgfsdh
7 mins ago
@sdgfsdh looks like"varsIgnorePattern": "^_"
should be the default eslint configuration
– Bergi
5 mins ago
 |Â
show 1 more comment
2
Maybe for what you need with anreturn xs.length > 0
is enough for you.
– Jesús Magallón
15 mins ago
2
@JesúsMagallón - Not all iterables have alength
property.
– T.J. Crowder
15 mins ago
Would eslint be happy if you named the variable_
?
– Bergi
10 mins ago
@Bergi sadly this isn't ML
– sdgfsdh
7 mins ago
@sdgfsdh looks like"varsIgnorePattern": "^_"
should be the default eslint configuration
– Bergi
5 mins ago
2
2
Maybe for what you need with an
return xs.length > 0
is enough for you.– Jesús Magallón
15 mins ago
Maybe for what you need with an
return xs.length > 0
is enough for you.– Jesús Magallón
15 mins ago
2
2
@JesúsMagallón - Not all iterables have a
length
property.– T.J. Crowder
15 mins ago
@JesúsMagallón - Not all iterables have a
length
property.– T.J. Crowder
15 mins ago
Would eslint be happy if you named the variable
_
?– Bergi
10 mins ago
Would eslint be happy if you named the variable
_
?– Bergi
10 mins ago
@Bergi sadly this isn't ML
– sdgfsdh
7 mins ago
@Bergi sadly this isn't ML
– sdgfsdh
7 mins ago
@sdgfsdh looks like
"varsIgnorePattern": "^_"
should be the default eslint configuration– Bergi
5 mins ago
@sdgfsdh looks like
"varsIgnorePattern": "^_"
should be the default eslint configuration– Bergi
5 mins ago
 |Â
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
6
down vote
No, you can't. You can use the iterator¹ directly, though:
const hasAny = xs => !xs[Symbol.iterator]().next().done;
Or if you want to ensure you proactively release any resources the iterator holds (rather than waiting for them to be released automatically — think DB connection in a generator function or similar):
const hasAny = xs =>
const it = xs[Symbol.iterator]();
const result = it.next().done;
if (it.return) it.return();
return result;
;
Live Example:
const hasAny = xs => !xs[Symbol.iterator]().next().done;
console.log(hasAny()); // false
console.log(hasAny([1])); // true
¹ (For any who don't happen to know, for-of
uses an iterator under the covers.)
//FIXME this doesn't .return() the iterator to close resources
– Bergi
13 mins ago
@Bergi - Meh, it'll get released immediately by any half-decent engine. But I'll add an option.
– T.J. Crowder
12 mins ago
I didn't mean anything getting garbage collected, I meant e.g. generator functions with afinally
clause that actively closes (not just forgets) a connection, rejects a transactions or something.
– Bergi
8 mins ago
@Bergi - Oh, I understand. Eventually GC should get to all of those, since nothing can guarantee proactive cleanup. Still, added the option.
– T.J. Crowder
6 mins ago
Assuming a network connection that needs to be explicitly closed, the event handler that still waits for packets does itself prevent the connection instance from being garbage-collected. Sure, many constructs don't guarantee active termination, but some cases require them - and would leak otherwise.
– Bergi
34 secs ago
add a comment |Â
up vote
1
down vote
According to ESLint issue #2173, you can set a comment to ignore the unused variable. For that, use the following code:
var some_unused_variable; // eslint-disable-line no-unused-vars
A community member of ESLint also states:
We now have a
varsIgnorePattern
option available for this use case (best suited for ignoring specific unused variable names or patterns across all your files).
And documentation states:
The varsIgnorePattern option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain ignored or Ignored.
Examples of correct code for the "varsIgnorePattern": "[iI]gnored" option:
/*eslint no-unused-vars: ["error", "varsIgnorePattern": "[iI]gnored" ]*/
var firstVarIgnored = 1;
var secondVar = 2;
console.log(secondVar);
1
This is true, but it's not an answer to the question. The question doesn't ask how to make ESLint stop complaining. It asks if you can avoid declaring a variable in afor-of
loop.
– T.J. Crowder
2 mins ago
add a comment |Â
up vote
0
down vote
You can use destructuring with an empty object:
const hasAny = xs =>
for (const of xs)
return true;
return false;
;
UPDATE: This does not work when the list contains undefined
.
the real beauty of javascript :)
– Isitar
12 mins ago
1
Nope, this doesn't work for anything that is not an iterable of iterables. TryhasAny([1])
– Bergi
12 mins ago
@Bergi my mistake, updated to use
– sdgfsdh
9 mins ago
1
... which doesn't work withundefined
.
– sdgfsdh
8 mins ago
2
Now it fails if the iterable's first value isnull
orundefined
.
– T.J. Crowder
8 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
No, you can't. You can use the iterator¹ directly, though:
const hasAny = xs => !xs[Symbol.iterator]().next().done;
Or if you want to ensure you proactively release any resources the iterator holds (rather than waiting for them to be released automatically — think DB connection in a generator function or similar):
const hasAny = xs =>
const it = xs[Symbol.iterator]();
const result = it.next().done;
if (it.return) it.return();
return result;
;
Live Example:
const hasAny = xs => !xs[Symbol.iterator]().next().done;
console.log(hasAny()); // false
console.log(hasAny([1])); // true
¹ (For any who don't happen to know, for-of
uses an iterator under the covers.)
//FIXME this doesn't .return() the iterator to close resources
– Bergi
13 mins ago
@Bergi - Meh, it'll get released immediately by any half-decent engine. But I'll add an option.
– T.J. Crowder
12 mins ago
I didn't mean anything getting garbage collected, I meant e.g. generator functions with afinally
clause that actively closes (not just forgets) a connection, rejects a transactions or something.
– Bergi
8 mins ago
@Bergi - Oh, I understand. Eventually GC should get to all of those, since nothing can guarantee proactive cleanup. Still, added the option.
– T.J. Crowder
6 mins ago
Assuming a network connection that needs to be explicitly closed, the event handler that still waits for packets does itself prevent the connection instance from being garbage-collected. Sure, many constructs don't guarantee active termination, but some cases require them - and would leak otherwise.
– Bergi
34 secs ago
add a comment |Â
up vote
6
down vote
No, you can't. You can use the iterator¹ directly, though:
const hasAny = xs => !xs[Symbol.iterator]().next().done;
Or if you want to ensure you proactively release any resources the iterator holds (rather than waiting for them to be released automatically — think DB connection in a generator function or similar):
const hasAny = xs =>
const it = xs[Symbol.iterator]();
const result = it.next().done;
if (it.return) it.return();
return result;
;
Live Example:
const hasAny = xs => !xs[Symbol.iterator]().next().done;
console.log(hasAny()); // false
console.log(hasAny([1])); // true
¹ (For any who don't happen to know, for-of
uses an iterator under the covers.)
//FIXME this doesn't .return() the iterator to close resources
– Bergi
13 mins ago
@Bergi - Meh, it'll get released immediately by any half-decent engine. But I'll add an option.
– T.J. Crowder
12 mins ago
I didn't mean anything getting garbage collected, I meant e.g. generator functions with afinally
clause that actively closes (not just forgets) a connection, rejects a transactions or something.
– Bergi
8 mins ago
@Bergi - Oh, I understand. Eventually GC should get to all of those, since nothing can guarantee proactive cleanup. Still, added the option.
– T.J. Crowder
6 mins ago
Assuming a network connection that needs to be explicitly closed, the event handler that still waits for packets does itself prevent the connection instance from being garbage-collected. Sure, many constructs don't guarantee active termination, but some cases require them - and would leak otherwise.
– Bergi
34 secs ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
No, you can't. You can use the iterator¹ directly, though:
const hasAny = xs => !xs[Symbol.iterator]().next().done;
Or if you want to ensure you proactively release any resources the iterator holds (rather than waiting for them to be released automatically — think DB connection in a generator function or similar):
const hasAny = xs =>
const it = xs[Symbol.iterator]();
const result = it.next().done;
if (it.return) it.return();
return result;
;
Live Example:
const hasAny = xs => !xs[Symbol.iterator]().next().done;
console.log(hasAny()); // false
console.log(hasAny([1])); // true
¹ (For any who don't happen to know, for-of
uses an iterator under the covers.)
No, you can't. You can use the iterator¹ directly, though:
const hasAny = xs => !xs[Symbol.iterator]().next().done;
Or if you want to ensure you proactively release any resources the iterator holds (rather than waiting for them to be released automatically — think DB connection in a generator function or similar):
const hasAny = xs =>
const it = xs[Symbol.iterator]();
const result = it.next().done;
if (it.return) it.return();
return result;
;
Live Example:
const hasAny = xs => !xs[Symbol.iterator]().next().done;
console.log(hasAny()); // false
console.log(hasAny([1])); // true
¹ (For any who don't happen to know, for-of
uses an iterator under the covers.)
const hasAny = xs => !xs[Symbol.iterator]().next().done;
console.log(hasAny()); // false
console.log(hasAny([1])); // true
const hasAny = xs => !xs[Symbol.iterator]().next().done;
console.log(hasAny()); // false
console.log(hasAny([1])); // true
edited 6 mins ago
answered 17 mins ago
T.J. Crowder
657k11311591257
657k11311591257
//FIXME this doesn't .return() the iterator to close resources
– Bergi
13 mins ago
@Bergi - Meh, it'll get released immediately by any half-decent engine. But I'll add an option.
– T.J. Crowder
12 mins ago
I didn't mean anything getting garbage collected, I meant e.g. generator functions with afinally
clause that actively closes (not just forgets) a connection, rejects a transactions or something.
– Bergi
8 mins ago
@Bergi - Oh, I understand. Eventually GC should get to all of those, since nothing can guarantee proactive cleanup. Still, added the option.
– T.J. Crowder
6 mins ago
Assuming a network connection that needs to be explicitly closed, the event handler that still waits for packets does itself prevent the connection instance from being garbage-collected. Sure, many constructs don't guarantee active termination, but some cases require them - and would leak otherwise.
– Bergi
34 secs ago
add a comment |Â
//FIXME this doesn't .return() the iterator to close resources
– Bergi
13 mins ago
@Bergi - Meh, it'll get released immediately by any half-decent engine. But I'll add an option.
– T.J. Crowder
12 mins ago
I didn't mean anything getting garbage collected, I meant e.g. generator functions with afinally
clause that actively closes (not just forgets) a connection, rejects a transactions or something.
– Bergi
8 mins ago
@Bergi - Oh, I understand. Eventually GC should get to all of those, since nothing can guarantee proactive cleanup. Still, added the option.
– T.J. Crowder
6 mins ago
Assuming a network connection that needs to be explicitly closed, the event handler that still waits for packets does itself prevent the connection instance from being garbage-collected. Sure, many constructs don't guarantee active termination, but some cases require them - and would leak otherwise.
– Bergi
34 secs ago
//FIXME this doesn't .return() the iterator to close resources
– Bergi
13 mins ago
//FIXME this doesn't .return() the iterator to close resources
– Bergi
13 mins ago
@Bergi - Meh, it'll get released immediately by any half-decent engine. But I'll add an option.
– T.J. Crowder
12 mins ago
@Bergi - Meh, it'll get released immediately by any half-decent engine. But I'll add an option.
– T.J. Crowder
12 mins ago
I didn't mean anything getting garbage collected, I meant e.g. generator functions with a
finally
clause that actively closes (not just forgets) a connection, rejects a transactions or something.– Bergi
8 mins ago
I didn't mean anything getting garbage collected, I meant e.g. generator functions with a
finally
clause that actively closes (not just forgets) a connection, rejects a transactions or something.– Bergi
8 mins ago
@Bergi - Oh, I understand. Eventually GC should get to all of those, since nothing can guarantee proactive cleanup. Still, added the option.
– T.J. Crowder
6 mins ago
@Bergi - Oh, I understand. Eventually GC should get to all of those, since nothing can guarantee proactive cleanup. Still, added the option.
– T.J. Crowder
6 mins ago
Assuming a network connection that needs to be explicitly closed, the event handler that still waits for packets does itself prevent the connection instance from being garbage-collected. Sure, many constructs don't guarantee active termination, but some cases require them - and would leak otherwise.
– Bergi
34 secs ago
Assuming a network connection that needs to be explicitly closed, the event handler that still waits for packets does itself prevent the connection instance from being garbage-collected. Sure, many constructs don't guarantee active termination, but some cases require them - and would leak otherwise.
– Bergi
34 secs ago
add a comment |Â
up vote
1
down vote
According to ESLint issue #2173, you can set a comment to ignore the unused variable. For that, use the following code:
var some_unused_variable; // eslint-disable-line no-unused-vars
A community member of ESLint also states:
We now have a
varsIgnorePattern
option available for this use case (best suited for ignoring specific unused variable names or patterns across all your files).
And documentation states:
The varsIgnorePattern option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain ignored or Ignored.
Examples of correct code for the "varsIgnorePattern": "[iI]gnored" option:
/*eslint no-unused-vars: ["error", "varsIgnorePattern": "[iI]gnored" ]*/
var firstVarIgnored = 1;
var secondVar = 2;
console.log(secondVar);
1
This is true, but it's not an answer to the question. The question doesn't ask how to make ESLint stop complaining. It asks if you can avoid declaring a variable in afor-of
loop.
– T.J. Crowder
2 mins ago
add a comment |Â
up vote
1
down vote
According to ESLint issue #2173, you can set a comment to ignore the unused variable. For that, use the following code:
var some_unused_variable; // eslint-disable-line no-unused-vars
A community member of ESLint also states:
We now have a
varsIgnorePattern
option available for this use case (best suited for ignoring specific unused variable names or patterns across all your files).
And documentation states:
The varsIgnorePattern option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain ignored or Ignored.
Examples of correct code for the "varsIgnorePattern": "[iI]gnored" option:
/*eslint no-unused-vars: ["error", "varsIgnorePattern": "[iI]gnored" ]*/
var firstVarIgnored = 1;
var secondVar = 2;
console.log(secondVar);
1
This is true, but it's not an answer to the question. The question doesn't ask how to make ESLint stop complaining. It asks if you can avoid declaring a variable in afor-of
loop.
– T.J. Crowder
2 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
According to ESLint issue #2173, you can set a comment to ignore the unused variable. For that, use the following code:
var some_unused_variable; // eslint-disable-line no-unused-vars
A community member of ESLint also states:
We now have a
varsIgnorePattern
option available for this use case (best suited for ignoring specific unused variable names or patterns across all your files).
And documentation states:
The varsIgnorePattern option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain ignored or Ignored.
Examples of correct code for the "varsIgnorePattern": "[iI]gnored" option:
/*eslint no-unused-vars: ["error", "varsIgnorePattern": "[iI]gnored" ]*/
var firstVarIgnored = 1;
var secondVar = 2;
console.log(secondVar);
According to ESLint issue #2173, you can set a comment to ignore the unused variable. For that, use the following code:
var some_unused_variable; // eslint-disable-line no-unused-vars
A community member of ESLint also states:
We now have a
varsIgnorePattern
option available for this use case (best suited for ignoring specific unused variable names or patterns across all your files).
And documentation states:
The varsIgnorePattern option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain ignored or Ignored.
Examples of correct code for the "varsIgnorePattern": "[iI]gnored" option:
/*eslint no-unused-vars: ["error", "varsIgnorePattern": "[iI]gnored" ]*/
var firstVarIgnored = 1;
var secondVar = 2;
console.log(secondVar);
answered 9 mins ago
E. Zacarias
7510
7510
1
This is true, but it's not an answer to the question. The question doesn't ask how to make ESLint stop complaining. It asks if you can avoid declaring a variable in afor-of
loop.
– T.J. Crowder
2 mins ago
add a comment |Â
1
This is true, but it's not an answer to the question. The question doesn't ask how to make ESLint stop complaining. It asks if you can avoid declaring a variable in afor-of
loop.
– T.J. Crowder
2 mins ago
1
1
This is true, but it's not an answer to the question. The question doesn't ask how to make ESLint stop complaining. It asks if you can avoid declaring a variable in a
for-of
loop.– T.J. Crowder
2 mins ago
This is true, but it's not an answer to the question. The question doesn't ask how to make ESLint stop complaining. It asks if you can avoid declaring a variable in a
for-of
loop.– T.J. Crowder
2 mins ago
add a comment |Â
up vote
0
down vote
You can use destructuring with an empty object:
const hasAny = xs =>
for (const of xs)
return true;
return false;
;
UPDATE: This does not work when the list contains undefined
.
the real beauty of javascript :)
– Isitar
12 mins ago
1
Nope, this doesn't work for anything that is not an iterable of iterables. TryhasAny([1])
– Bergi
12 mins ago
@Bergi my mistake, updated to use
– sdgfsdh
9 mins ago
1
... which doesn't work withundefined
.
– sdgfsdh
8 mins ago
2
Now it fails if the iterable's first value isnull
orundefined
.
– T.J. Crowder
8 mins ago
add a comment |Â
up vote
0
down vote
You can use destructuring with an empty object:
const hasAny = xs =>
for (const of xs)
return true;
return false;
;
UPDATE: This does not work when the list contains undefined
.
the real beauty of javascript :)
– Isitar
12 mins ago
1
Nope, this doesn't work for anything that is not an iterable of iterables. TryhasAny([1])
– Bergi
12 mins ago
@Bergi my mistake, updated to use
– sdgfsdh
9 mins ago
1
... which doesn't work withundefined
.
– sdgfsdh
8 mins ago
2
Now it fails if the iterable's first value isnull
orundefined
.
– T.J. Crowder
8 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can use destructuring with an empty object:
const hasAny = xs =>
for (const of xs)
return true;
return false;
;
UPDATE: This does not work when the list contains undefined
.
You can use destructuring with an empty object:
const hasAny = xs =>
for (const of xs)
return true;
return false;
;
UPDATE: This does not work when the list contains undefined
.
edited 5 mins ago
answered 15 mins ago


sdgfsdh
7,18863581
7,18863581
the real beauty of javascript :)
– Isitar
12 mins ago
1
Nope, this doesn't work for anything that is not an iterable of iterables. TryhasAny([1])
– Bergi
12 mins ago
@Bergi my mistake, updated to use
– sdgfsdh
9 mins ago
1
... which doesn't work withundefined
.
– sdgfsdh
8 mins ago
2
Now it fails if the iterable's first value isnull
orundefined
.
– T.J. Crowder
8 mins ago
add a comment |Â
the real beauty of javascript :)
– Isitar
12 mins ago
1
Nope, this doesn't work for anything that is not an iterable of iterables. TryhasAny([1])
– Bergi
12 mins ago
@Bergi my mistake, updated to use
– sdgfsdh
9 mins ago
1
... which doesn't work withundefined
.
– sdgfsdh
8 mins ago
2
Now it fails if the iterable's first value isnull
orundefined
.
– T.J. Crowder
8 mins ago
the real beauty of javascript :)
– Isitar
12 mins ago
the real beauty of javascript :)
– Isitar
12 mins ago
1
1
Nope, this doesn't work for anything that is not an iterable of iterables. Try
hasAny([1])
– Bergi
12 mins ago
Nope, this doesn't work for anything that is not an iterable of iterables. Try
hasAny([1])
– Bergi
12 mins ago
@Bergi my mistake, updated to use
– sdgfsdh
9 mins ago
@Bergi my mistake, updated to use
– sdgfsdh
9 mins ago
1
1
... which doesn't work with
undefined
.– sdgfsdh
8 mins ago
... which doesn't work with
undefined
.– sdgfsdh
8 mins ago
2
2
Now it fails if the iterable's first value is
null
or undefined
.– T.J. Crowder
8 mins ago
Now it fails if the iterable's first value is
null
or undefined
.– T.J. Crowder
8 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52667690%2fis-is-possible-to-create-a-for-of-loop-without-a-variable%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Maybe for what you need with an
return xs.length > 0
is enough for you.– Jesús Magallón
15 mins ago
2
@JesúsMagallón - Not all iterables have a
length
property.– T.J. Crowder
15 mins ago
Would eslint be happy if you named the variable
_
?– Bergi
10 mins ago
@Bergi sadly this isn't ML
– sdgfsdh
7 mins ago
@sdgfsdh looks like
"varsIgnorePattern": "^_"
should be the default eslint configuration– Bergi
5 mins ago