最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - How to do an inline for loop in JavaScript, that's not connected to a variable - Stack Overflow

programmeradmin7浏览0评论

It's known that in general, JavaScript allows an inline for loop in the format:

someArray.forEach(x => x.doSomething());

However, when one wants to use a regular for-loop inline, as one statement, an error is occured. For example:

void(for(var i = 0; i < 0; i++) console.log(i));

Even though this is technically one line, but since it's used in a format of literally being inline, and being considered one statement, it gives the error:

Uncaught SyntaxError: Unexpected token 'for'

Why might one want to do this? Simple: in order to generate an array, or string, in one line, for example:

var newString = (let k = "", for(let i = 0; i < 1000; i++) k+= i, k);

But this gives an obvious

Uncaught SyntaxError: Unexpected identifier

error, because of the "let" keyword, but that's a different question.

Mainly, is it possible to make a regular for-loop inline in JavaScript?

It's known that in general, JavaScript allows an inline for loop in the format:

someArray.forEach(x => x.doSomething());

However, when one wants to use a regular for-loop inline, as one statement, an error is occured. For example:

void(for(var i = 0; i < 0; i++) console.log(i));

Even though this is technically one line, but since it's used in a format of literally being inline, and being considered one statement, it gives the error:

Uncaught SyntaxError: Unexpected token 'for'

Why might one want to do this? Simple: in order to generate an array, or string, in one line, for example:

var newString = (let k = "", for(let i = 0; i < 1000; i++) k+= i, k);

But this gives an obvious

Uncaught SyntaxError: Unexpected identifier

error, because of the "let" keyword, but that's a different question.

Mainly, is it possible to make a regular for-loop inline in JavaScript?

Share Improve this question asked Apr 7, 2020 at 10:58 B''H Bi'ezras -- Boruch HashemB''H Bi'ezras -- Boruch Hashem 1 2
  • You can always use an IIFE. – Bergi Commented Apr 7, 2020 at 11:08
  • @Bergi whats that – B''H Bi'ezras -- Boruch Hashem Commented Apr 7, 2020 at 11:08
Add a comment  | 

3 Answers 3

Reset to default 9

Here is a single line IIFE (Immediately Invoked Function Expression):

let newString = (() => {let concatenatedString = ''; for (let i = 0; i < 1000; i++) {concatenatedString+= i;} return concatenatedString;})();

console.log(newString);


Further Reading on IIFEs:

  • https://developer.mozilla.org/en-US/docs/Glossary/IIFE

for is a statement not a expression you can't use it at right hand side of variable assignment. You also don't have to use void. Just simply use the loop

for(var i = 0; i < 4; i++) console.log(i)

Why would you specifically need a for loop in one line? It looks less readable. Why not just warp your for loop in a function?

function functionName(number) {
    let temp = 0;
    let i = 0;

    for (i; i < number; i++) {
        temp += i;
    }

    return temp;
}

const value = functionName(5);

console.log(value);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论