Skip to content

Instantly share code, notes, and snippets.

@wiledal
Last active March 13, 2023 22:47
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save wiledal/3c5b63887cc8a010a330b89aacff2f2e to your computer and use it in GitHub Desktop.
Save wiledal/3c5b63887cc8a010a330b89aacff2f2e to your computer and use it in GitHub Desktop.
Template Literals example: For loops
/*
Template literals for-loop example
Using `Array(5).join(0).split(0)`, we create an empty array
with 5 items which we can iterate through using `.map()`
*/
var element = document.createElement('div')
element.innerHTML = `
<h1>This element is looping</h1>
${Array(5).join(0).split(0).map((item, i) => `
<div>I am item number ${i}.</div>
`).join('')}
`
/*
Results:
<div>
<h1>This element is looping</h1>
<div>I am item number 0.</div>
<div>I am item number 1.</div>
<div>I am item number 2.</div>
<div>I am item number 3.</div>
<div>I am item number 4.</div>
</div>
*/
@naimur2001
Copy link

Thanks WILEDAL;

@wiledal
Copy link
Author

wiledal commented Mar 3, 2023

@bacloud23

When you use Map, you like it because it looks cool and code is shorter. But it is not it's purpose and any other developer would not like it.

Except, you know, the entire React community 👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment