The condition test occurs before statement in the loop is Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. In the next tutorial, you will learn about while and dowhile loop. Loops can execute a block of code a number of times. iteration hook with statements to be executed for the value of each distinct property. When i becomes 101, the test condition is false and sum will be equal to 0 + 1 + 2 + + 100. Each approach produces the exact same result: Each approach prints the numbers array this way: Today you learned about the for loops in JavaScript. When the condition is false at the start of the iteration, the loop will stop executing. The following for cycle calculates the offset position of a node in the afterthought section, and therefore it does not require the use of a statement section, a semicolon is used instead. If you think about a regular while loop, if the condition is false, to begin with, the loop does not execute once. The same three items are still present, and they are still defined in the same order as they are in the for loop. Why do javascript implementations parse numbers this way? Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. Syntax: for (first expression; second expression; third expression ) { // statements to be executed repeatedly } Here, the first expression is executed before the loop starts. The following for statement starts by declaring the variable i and initializing it to 0. There are many different types of for loops in JavaScript, but the most basic ones look like this: for ( initialization of expression; condition; action for initialized expression ) { instruction statement to be executed; } If you are omitting this expression, you must make sure to break the loop in the body in order to not create an infinite loop. before the loop starts): Often expression 2 is used to evaluate the condition of the initial variable. This piece of code is always executed at least once. For other uses, for, while, and dowhile loops are largely interchangeable. statement following the loop. Share The keyword directly precedes the curly braces containing the code to run and the final expression. no way of knowing how many guesses it will take. Inside the loop, you then use this key to access the value for that key in the data object. Normally used to initialize a counter variable. If the number is 0, print "Blast off!" Executed before the code block starts. If you have some one-based numberings in that array, just use i+1 where you need it; in your case 'item-'+ (i+1). Get started, freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546). also optional. if (person === 'Phil' || person === 'Lola') { How do laws against computer intrusion handle the modern situation of devices routinely being under the de facto control of non-owners? It's the same with loops a break statement will immediately exit the loop and make the browser move on to any code that follows it. This is why there is a special variation of a while loop called the do-while loop. Each time through the loop, . refused.textContent += \`\${person}, \`; 586), Starting the Prompt Design Site: A New Home in our Stack Exchange Neighborhood, Testing native, sponsored banner ads on Stack Overflow (starting July 6), Temporary policy: Generative AI (e.g., ChatGPT) is banned, Javascript iterations - keep same number until next run, Javascript loop, counter equals 0 two times in a loop, Free the counter variable when a while loop exits in JavaScript, How to stop a while loop after looping x times, Decrementing While Loop ordering Javascript, Javascript counter controlled loop using (while) error, How to get the counter to start from 0 again in JavaScript, Do profinite groups admit maximal subgroups. that did it, thanks very much. reserved word. points. The JavaScript for loop is similar to the Java and C for loop. Do you want to create apps with an outstanding design? You've reached the end of this article, but can you remember the most important information? Warning: With while and dowhile as with all loops you must make sure that the initializer is incremented or, depending on the case, decremented, so the condition eventually becomes false. Does the EMF of a battery change with time? This does not log "0, 1, 2", like what would happen if getI is declared in the loop body. Also, you can use break to exit the loop early, before the condition expression evaluates to false. Programming languages are very useful for rapidly completing repetitive tasks, from multiple basic calculations to just about any other situation where you've got a lot of similar items of work to complete. Let's rewrite our cat listing example again to use a dowhile loop: Note: Again, this works just the same as expected have a look at it running live on GitHub (also view the full source code). For example, given a target number and a list of numbers, lets build a for loop to find the target: We used the break statement to escape the loop right when the target number was found. Looping in general is useful in many ways in programming. A for loop repeats until a specified condition evaluates to false. The statement that you identify with a label may be How to set while loop counter to start at 1, not 0. This occurs before the next evaluation of condition. Creates a loop without needing to call a method on an array. An expression to be evaluated before each loop iteration. In JavaScript, a for loop makes it possible to repeat code as long as a condition is met. If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail: for (let i = 0, len = cars.length, text = ""; i < len; i++) {, W3Schools is optimized for learning and training. To loop through an array, you need to be careful. For instance, you can have an array of values and use a loop to access one value at a time. Content available under a Creative Commons license. operator, SyntaxError: redeclaration of formal parameter "x". the loop does not redeclare the variable outside the loop. Find centralized, trusted content and collaborate around the technologies you use most. Then we gave the loop a condition to terminate the loop once the value of the counter variable ( i) is less than ( <) the length of . } else { Optional. optional. Here the age is 50, to begin with. Next, lets take a look at a more aggressive control flow statement, break. This page was last modified on Jul 3, 2023 by MDN contributors. How to maximize the monthly 1:1 meeting with my boss? Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff. the idea "Go five steps to the east" could be expressed this way as a loop: There are many different kinds of loops, but they all essentially do the same thing: To subscribe to this RSS feed, copy and paste this URL into your RSS reader. statement that executes when the value of i is 3. We can also decide to use variables to set the max number of our condition this way: This can hang your system, because it continues to run until the memory is full, since the condition always evaluates as true. take X steps in one direction, then Y steps in another. Other solutions were possible, but are counter-intuitive. For example, in the code below we want to log a message listing our cats: The final output sentence isn't very well-formed: We'd prefer it to handle the last cat differently, like this: But to do this we need to know when we are on the final loop iteration, and to do that we can use a for loop and examine the value of i: If you want to exit a loop before all the iterations have been completed, you can use the break statement. i=array.length-1, i>=0; i-- (except clarity of course :D) For example, lets create a loop that finds a target number given an array of numbers: In this loop, the condition consists of two parts: If both of these conditions are true, the looping continues. If the condition is false, the for loop is terminated. In a case like that, you should use the for loop. This loop starts us at 0, increases the variable by one each loop, and stops when we hit the last element in the array. Variables declared with let are local to the statement. The reason is that each setTimeout creates a new closure that closes over the i variable, but if the i is not scoped to the loop body, all closures will reference the same variable when they eventually get called and due to the asynchronous nature of setTimeout, it will happen after the loop has already exited, causing the value of i in all queued callbacks' bodies to have the value of 3. First story to suggest some successor to steam power? The 2.2-mile course includes 12 narrow turns along a winding mix of concrete, asphalt and elevation. The continue statement allows you to skip the rest of the code in the loop. The do-while loop is very similar to a while loop. You can start a loop at whatever you want. if (i === 10) { Parewa Labs Pvt. In this case, the input should be a number (, Inside the loop, we find the square root of each number using, If the square root and the rounded down square root do not equal one another (, If the square root is an integer, we skip past the, Loop from 10 down to 0. You can use this method to loop through the elements of an array without having to write down a for loop or a while loop. To execute multiple statements, use a block statement ({ }) to group A while statement looks checked. However, if you start at zero, remember to be aware that the arrays still start at zero, and you will need an n + 1 size array to represent n elements if you use indexing starting at 1. Here are 5 ways to print the numbers of an array. We have two lists, one for guests to admit, and one for guests to refuse. Im bringing this up because it can sometimes cause confusion. Get tips on how to become a job-ready software developer in no time. When did a Prime Minister last miss two, consecutive Prime Minister's Questions? They can all be used to solve the same problems, and which one you use will largely depend on your personal preference which one you find easiest to remember or most intuitive. The loop automatically increments i for each run. If you have a single-line expression in a for loop, you do not need to use curly brackets. Multiple let and var declarations can also be joined with commas. output.appendChild(para); arguments object and so on), invoking a custom (Note that it's possible that number could Lets start by displaying some text several times until our condition is met. An iterable means a collection of values that can be looped through. While forin iterates Does the EMF of a battery change with time? Safe to drive back home with torn ball joint boot? Otherwise, the for statement acquires the following console.log line as its statement section, which makes the log execute multiple times. Or are you curious about how to create a successful mobile app? while, do-while, for, or label [duplicate]. If you're iterating through an array or some other object that supports it, and don't need access to the index position of each item, then forof is the best choice. If the condition returns true, statement is executed The reason you see loops starting at zero often, is because they are looping through an array. Over time you will find that most data structures, methods and programming routines that you work with treat arrays as being zero-based. Now the continue statement prevents the execution of the last line in the for loop. The second form of the syntax terminates the specified enclosing labeled statement. Lets use a for loop to log values from 1 to 10 one by one. When did a Prime Minister last miss two, consecutive Prime Minister's Questions? You will learn about the other type of loops in the upcoming tutorials. Choosing the right type of AI art generator is crucial to produce unique, original, and professional artwork. Extra bonus question after completing the above tasks successfully, you will be left with two lists of names, separated by commas, but they will be untidy there will be a comma at the end of each one. Follow our guided path, With our online code editor, you can edit code and view the result in your browser, Join one of our online bootcamps and learn from experienced instructors, We have created a bunch of responsive website templates you can use - for free, Large collection of code snippets for HTML, CSS and JavaScript, Learn the basics of HTML in a fun and engaging video tutorial, Build fast and responsive sites using our free W3.CSS framework, Host your own website, and share it to the world with W3Schools Spaces. Is Linux swap still needed with Ubuntu 22.04, Overvoltage protection with ultra low leakage current for 3.3 V. Is there a finite abelian group which is not isomorphic to either the additive or multiplicative group of a field? To take home, you can use the continue statement to skip the rest of a loop. Stone-Weierstrass theorem for non-polynomials, Circle and arrow on a single term of a math equation - arrow up and down, Plot multiple lines along with converging dotted line. In the second example, using let, the variable declared in Given a data object, lets iterate through its keys and print the key-value pairs to the console: Here the for-in loop takes one key at a time and assigns it to a variable called key. with accepted and upvoted answer being "Start your for loops where ever you have to start them. specified condition evaluates to true. When the feature can combine multiple other language features. statement iterates over user-defined properties in addition to the array elements, if A for loop is a type of loop that repeats a block of code a specific number of times. When the condition becomes false, the loop terminates. Help the lynx collect pine cones, Join our newsletter and get access to exclusive content every month. For example, let's display numbers from 0 to 5 using a for loop: for(let i = 0; i <= 5; i+=1) { console.log(i) } Output: 0 1 2 3 4 5 I've tried a few things but I'm missing this. You can use the variable i in the code to be executed during the loop. In the following example, the do loop iterates at least once and Previously, you saw examples where the looping condition is simple. Loops are a programming concept that we constantly encounter and implement as JavaScript developers. If you get really stuck, press "Show solution" to see a solution. Note that the semicolon after the for statement is mandatory, because it stands as an empty statement. Condition: specify a condition that must evaluate to true . Making statements based on opinion; back them up with references or personal experience. Furthermore, no matter what the age is, lets print it at least once. Using function expressions we could rewrite the example above to be much more compact: In the "drawing circles" example above, you don't have a collection of items to loop through: you really just want to run the same code 100 times. Time to start the next iteration. Generally used to update or increment the counter variable. More precisely, let declarations are special-cased by for loops if initialization is a let declaration, then every time, after the loop body is evaluated, the following happens: So re-assigning the new variables within afterthought does not affect the bindings from the previous iteration. Now you have a solid understanding of for loops in JavaScript. These courses prepare you Are you looking to become a professional Python developer? Anybody? If you want to go further with your loop learning, read our advanced Loops and iteration guide. The result of this expression is discarded. This expression may optionally declare new variables with var or let keywords. A string is another example of an iterable data type that is commonly used. This conditional test is optional. This means if the condition starts as false, your loop will never execute. Instead, you get a single dashed line after the very last element. For each distinct property, Can you work out how to write lines that slice the last comma off in each case, and add a full stop to the end? In the above program, the condition is always true which will then run the code for infinite times. Lets take a look at the syntax of the for-of loop. Although both ways are correct, you should try to make your code more readable. SyntaxError: test for equality (==) mistyped as assignment (=)? In other words, you can use a for loop to loop through strings as well. Each of these loops can be used to accomplish the same tasks with different syntax. const para = document.createElement('p'); Then inside the loop, we're using i to access each item in the array in turn. Let's look at another example that takes a number as an input, and returns only the numbers that are squares of integers (whole numbers). The syntax of the for loop is: for (initialExpression; condition; updateExpression) { // for loop body } Here, The initialExpression initializes and/or declares variables and executes only once. First, some simple HTML a text allowing us to enter a name to search for, a
for loop start at 1 javascriptrv park old town scottsdale
The condition test occurs before statement in the loop is Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. In the next tutorial, you will learn about while and dowhile loop. Loops can execute a block of code a number of times. iteration hook with statements to be executed for the value of each distinct property. When i becomes 101, the test condition is false and sum will be equal to 0 + 1 + 2 + + 100. Each approach produces the exact same result: Each approach prints the numbers array this way: Today you learned about the for loops in JavaScript. When the condition is false at the start of the iteration, the loop will stop executing. The following for cycle calculates the offset position of a node in the afterthought section, and therefore it does not require the use of a statement section, a semicolon is used instead. If you think about a regular while loop, if the condition is false, to begin with, the loop does not execute once. The same three items are still present, and they are still defined in the same order as they are in the for loop. Why do javascript implementations parse numbers this way? Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. Syntax: for (first expression; second expression; third expression ) { // statements to be executed repeatedly } Here, the first expression is executed before the loop starts. The following for statement starts by declaring the variable i and initializing it to 0. There are many different types of for loops in JavaScript, but the most basic ones look like this: for ( initialization of expression; condition; action for initialized expression ) { instruction statement to be executed; } If you are omitting this expression, you must make sure to break the loop in the body in order to not create an infinite loop. before the loop starts): Often expression 2 is used to evaluate the condition of the initial variable. This piece of code is always executed at least once. For other uses, for, while, and dowhile loops are largely interchangeable. statement following the loop. Share The keyword directly precedes the curly braces containing the code to run and the final expression. no way of knowing how many guesses it will take. Inside the loop, you then use this key to access the value for that key in the data object. Normally used to initialize a counter variable. If the number is 0, print "Blast off!" Executed before the code block starts. If you have some one-based numberings in that array, just use i+1 where you need it; in your case 'item-'+ (i+1). Get started, freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546). also optional. if (person === 'Phil' || person === 'Lola') { How do laws against computer intrusion handle the modern situation of devices routinely being under the de facto control of non-owners? It's the same with loops a break statement will immediately exit the loop and make the browser move on to any code that follows it. This is why there is a special variation of a while loop called the do-while loop. Each time through the loop, . refused.textContent += \`\${person}, \`; 586), Starting the Prompt Design Site: A New Home in our Stack Exchange Neighborhood, Testing native, sponsored banner ads on Stack Overflow (starting July 6), Temporary policy: Generative AI (e.g., ChatGPT) is banned, Javascript iterations - keep same number until next run, Javascript loop, counter equals 0 two times in a loop, Free the counter variable when a while loop exits in JavaScript, How to stop a while loop after looping x times, Decrementing While Loop ordering Javascript, Javascript counter controlled loop using (while) error, How to get the counter to start from 0 again in JavaScript, Do profinite groups admit maximal subgroups. that did it, thanks very much. reserved word. points. The JavaScript for loop is similar to the Java and C for loop. Do you want to create apps with an outstanding design? You've reached the end of this article, but can you remember the most important information? Warning: With while and dowhile as with all loops you must make sure that the initializer is incremented or, depending on the case, decremented, so the condition eventually becomes false. Does the EMF of a battery change with time? This does not log "0, 1, 2", like what would happen if getI is declared in the loop body. Also, you can use break to exit the loop early, before the condition expression evaluates to false. Programming languages are very useful for rapidly completing repetitive tasks, from multiple basic calculations to just about any other situation where you've got a lot of similar items of work to complete. Let's rewrite our cat listing example again to use a dowhile loop: Note: Again, this works just the same as expected have a look at it running live on GitHub (also view the full source code). For example, given a target number and a list of numbers, lets build a for loop to find the target: We used the break statement to escape the loop right when the target number was found. Looping in general is useful in many ways in programming. A for loop repeats until a specified condition evaluates to false. The statement that you identify with a label may be How to set while loop counter to start at 1, not 0. This occurs before the next evaluation of condition. Creates a loop without needing to call a method on an array. An expression to be evaluated before each loop iteration. In JavaScript, a for loop makes it possible to repeat code as long as a condition is met. If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail: for (let i = 0, len = cars.length, text = ""; i < len; i++) {, W3Schools is optimized for learning and training. To loop through an array, you need to be careful. For instance, you can have an array of values and use a loop to access one value at a time. Content available under a Creative Commons license. operator, SyntaxError: redeclaration of formal parameter "x". the loop does not redeclare the variable outside the loop. Find centralized, trusted content and collaborate around the technologies you use most. Then we gave the loop a condition to terminate the loop once the value of the counter variable ( i) is less than ( <) the length of . } else { Optional. optional. Here the age is 50, to begin with. Next, lets take a look at a more aggressive control flow statement, break. This page was last modified on Jul 3, 2023 by MDN contributors. How to maximize the monthly 1:1 meeting with my boss? Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff. the idea "Go five steps to the east" could be expressed this way as a loop: There are many different kinds of loops, but they all essentially do the same thing: To subscribe to this RSS feed, copy and paste this URL into your RSS reader. statement that executes when the value of i is 3. We can also decide to use variables to set the max number of our condition this way: This can hang your system, because it continues to run until the memory is full, since the condition always evaluates as true. take X steps in one direction, then Y steps in another. Other solutions were possible, but are counter-intuitive. For example, in the code below we want to log a message listing our cats: The final output sentence isn't very well-formed: We'd prefer it to handle the last cat differently, like this: But to do this we need to know when we are on the final loop iteration, and to do that we can use a for loop and examine the value of i: If you want to exit a loop before all the iterations have been completed, you can use the break statement. i=array.length-1, i>=0; i-- (except clarity of course :D) For example, lets create a loop that finds a target number given an array of numbers: In this loop, the condition consists of two parts: If both of these conditions are true, the looping continues. If the condition is false, the for loop is terminated. In a case like that, you should use the for loop. This loop starts us at 0, increases the variable by one each loop, and stops when we hit the last element in the array. Variables declared with let are local to the statement. The reason is that each setTimeout creates a new closure that closes over the i variable, but if the i is not scoped to the loop body, all closures will reference the same variable when they eventually get called and due to the asynchronous nature of setTimeout, it will happen after the loop has already exited, causing the value of i in all queued callbacks' bodies to have the value of 3. First story to suggest some successor to steam power? The 2.2-mile course includes 12 narrow turns along a winding mix of concrete, asphalt and elevation. The continue statement allows you to skip the rest of the code in the loop. The do-while loop is very similar to a while loop. You can start a loop at whatever you want. if (i === 10) { Parewa Labs Pvt. In this case, the input should be a number (, Inside the loop, we find the square root of each number using, If the square root and the rounded down square root do not equal one another (, If the square root is an integer, we skip past the, Loop from 10 down to 0. You can use this method to loop through the elements of an array without having to write down a for loop or a while loop. To execute multiple statements, use a block statement ({ }) to group A while statement looks checked. However, if you start at zero, remember to be aware that the arrays still start at zero, and you will need an n + 1 size array to represent n elements if you use indexing starting at 1. Here are 5 ways to print the numbers of an array. We have two lists, one for guests to admit, and one for guests to refuse. Im bringing this up because it can sometimes cause confusion. Get tips on how to become a job-ready software developer in no time. When did a Prime Minister last miss two, consecutive Prime Minister's Questions? They can all be used to solve the same problems, and which one you use will largely depend on your personal preference which one you find easiest to remember or most intuitive. The loop automatically increments i for each run. If you have a single-line expression in a for loop, you do not need to use curly brackets. Multiple let and var declarations can also be joined with commas. output.appendChild(para); arguments object and so on), invoking a custom (Note that it's possible that number could Lets start by displaying some text several times until our condition is met. An iterable means a collection of values that can be looped through. While forin iterates Does the EMF of a battery change with time? Safe to drive back home with torn ball joint boot? Otherwise, the for statement acquires the following console.log line as its statement section, which makes the log execute multiple times. Or are you curious about how to create a successful mobile app? while, do-while, for, or label [duplicate]. If you're iterating through an array or some other object that supports it, and don't need access to the index position of each item, then forof is the best choice. If the condition returns true, statement is executed The reason you see loops starting at zero often, is because they are looping through an array. Over time you will find that most data structures, methods and programming routines that you work with treat arrays as being zero-based. Now the continue statement prevents the execution of the last line in the for loop. The second form of the syntax terminates the specified enclosing labeled statement. Lets use a for loop to log values from 1 to 10 one by one. When did a Prime Minister last miss two, consecutive Prime Minister's Questions? You will learn about the other type of loops in the upcoming tutorials. Choosing the right type of AI art generator is crucial to produce unique, original, and professional artwork. Extra bonus question after completing the above tasks successfully, you will be left with two lists of names, separated by commas, but they will be untidy there will be a comma at the end of each one. Follow our guided path, With our online code editor, you can edit code and view the result in your browser, Join one of our online bootcamps and learn from experienced instructors, We have created a bunch of responsive website templates you can use - for free, Large collection of code snippets for HTML, CSS and JavaScript, Learn the basics of HTML in a fun and engaging video tutorial, Build fast and responsive sites using our free W3.CSS framework, Host your own website, and share it to the world with W3Schools Spaces. Is Linux swap still needed with Ubuntu 22.04, Overvoltage protection with ultra low leakage current for 3.3 V. Is there a finite abelian group which is not isomorphic to either the additive or multiplicative group of a field? To take home, you can use the continue statement to skip the rest of a loop. Stone-Weierstrass theorem for non-polynomials, Circle and arrow on a single term of a math equation - arrow up and down, Plot multiple lines along with converging dotted line. In the second example, using let, the variable declared in Given a data object, lets iterate through its keys and print the key-value pairs to the console: Here the for-in loop takes one key at a time and assigns it to a variable called key. with accepted and upvoted answer being "Start your for loops where ever you have to start them. specified condition evaluates to true. When the feature can combine multiple other language features. statement iterates over user-defined properties in addition to the array elements, if A for loop is a type of loop that repeats a block of code a specific number of times. When the condition becomes false, the loop terminates. Help the lynx collect pine cones, Join our newsletter and get access to exclusive content every month. For example, let's display numbers from 0 to 5 using a for loop: for(let i = 0; i <= 5; i+=1) { console.log(i) } Output: 0 1 2 3 4 5 I've tried a few things but I'm missing this. You can use the variable i in the code to be executed during the loop. In the following example, the do loop iterates at least once and Previously, you saw examples where the looping condition is simple. Loops are a programming concept that we constantly encounter and implement as JavaScript developers. If you get really stuck, press "Show solution" to see a solution. Note that the semicolon after the for statement is mandatory, because it stands as an empty statement. Condition: specify a condition that must evaluate to true . Making statements based on opinion; back them up with references or personal experience. Furthermore, no matter what the age is, lets print it at least once. Using function expressions we could rewrite the example above to be much more compact: In the "drawing circles" example above, you don't have a collection of items to loop through: you really just want to run the same code 100 times. Time to start the next iteration. Generally used to update or increment the counter variable. More precisely, let declarations are special-cased by for loops if initialization is a let declaration, then every time, after the loop body is evaluated, the following happens: So re-assigning the new variables within afterthought does not affect the bindings from the previous iteration. Now you have a solid understanding of for loops in JavaScript. These courses prepare you Are you looking to become a professional Python developer? Anybody? If you want to go further with your loop learning, read our advanced Loops and iteration guide. The result of this expression is discarded. This expression may optionally declare new variables with var or let keywords. A string is another example of an iterable data type that is commonly used. This conditional test is optional. This means if the condition starts as false, your loop will never execute. Instead, you get a single dashed line after the very last element. For each distinct property, Can you work out how to write lines that slice the last comma off in each case, and add a full stop to the end? In the above program, the condition is always true which will then run the code for infinite times. Lets take a look at the syntax of the for-of loop. Although both ways are correct, you should try to make your code more readable. SyntaxError: test for equality (==) mistyped as assignment (=)? In other words, you can use a for loop to loop through strings as well. Each of these loops can be used to accomplish the same tasks with different syntax. const para = document.createElement('p'); Then inside the loop, we're using i to access each item in the array in turn. Let's look at another example that takes a number as an input, and returns only the numbers that are squares of integers (whole numbers). The syntax of the for loop is: for (initialExpression; condition; updateExpression) { // for loop body } Here, The initialExpression initializes and/or declares variables and executes only once. First, some simple HTML a text allowing us to enter a name to search for, a