Js Why Can You Declare the Same Variable Again in a for Loop
In that location are diverse types of loops in JavaScript, and all of them essentially do the same affair: they repeat an action again and again.
Loops come in handy if yous desire to echo the same block of code for a certain number of times. Basically, they are a fast and effective way to echo something.
This article focuses on the for
loop in the JavaScript programming linguistic communication and goes over its basic syntax.
In improver, I'll explain how to loop through an array using a for
loop, which is a key programming concept.
What is a for loop? A bones syntax breakup
A for
loop repeats an activeness while a specific condition is true
. Information technology stops repeating the activeness when the condition finally evaluates to false
.
A for
loop in JavaScript looks very similar to a for
loop in C and Coffee.
In that location are many dissimilar types of for
loops in JavaScript, but the almost basic ones await like this:
for( initialization of expression; condition; action for initialized expression ) { instruction statement to be executed; }
This blazon of loop starts with the for
keyword, followed by a gear up of parentheses. Within them, at that place are three optional expression statements separated by a semicolon,;
. Lastly, at that place is a set of curly braces, {}
, that enclose the code block statement to be executed.
Hither's an example:
for (allow i = 0; i < ten; i++) { console.log('Counting numbers'); // runs and prints "Counting numbers" 10 times // values of i range from 0 to 9 }
In more detail, when a for
loop is executed:
- If there is any initial expression, it is run start and executed only once before any code in the block is run and earlier the loop starts. In this stride there is an initialization of one or more counters and declaration of ane or more variables used in the loop, like
permit i =0
. If there is more than i variable, they are separated by commas. - Side by side is the definition of the status expression that has to exist met in order for the loop to run,
i < ten
. As mentioned before, the instruction statements in the code cake will run only when this condition evaluates totrue
. If the value issimulated
the loop stops running. If at that place is no status it is alwaystruthful
which creates an infinite loop. - Then, the pedagogy statement inside the block with the curly braces,
{..}
, is executed. In that location can be more than one, on multiple lines. - Later each time the lawmaking block has been executed, there can be an action for the initialized expression that takes identify at the finish, like an increase argument (
i++
) that updates the initial expression. - Later on that, the condition is checked over again and if information technology evaluates to true the process is repeated.
What is an assortment?
An array is a data structure.
It is an ordered collection of multiple items, called elements, under the same proper name stored together in a listing. This then allows them to be easily sorted or searched through.
Arrays in JavaScript can incorporate elements with values of different information types. An array can contain numbers, strings, some other array, boolean values, and more – at the same time.
Indexing e'er starts at 0
. This ways that the first particular in an array is referenced with a zero index, the second detail has a one index, and the last item is the array length - ane
.
The easiest and most preferable manner to create an assortment is with array literal notation, which you can do with foursquare brackets with a comma separated list of elements, like the below assortment of strings:
let programmingLanguages = ["JavaScript","Java","Python","Ruby"];
To admission the starting time item nosotros utilise the alphabetize number:
console.log(programmingLanguages[0]); // prints JavaScript
How to Iterate Over an Assortment with a for loop
Each time the for
loop runs, it has a different value – and this is the case with arrays.
A for loop examines and iterates over every chemical element the array contains in a fast, effective, and more controllable way.
A basic example of looping through an array is:
const myNumbersArray = [ ane,2,3,iv,5]; for(let i = 0; i < myNumbersArray.length; i++) { console.log(myNumbersArray[i]); }
Output:
i 2 three 4 five
This is much more constructive than printing each value individually:
panel.log(myNumbersArray[0]); console.log(myNumbersArray[one]); panel.log(myNumbersArray[2]); console.log(myNumbersArray[3]); console.log(myNumbersArray[4]);
Let's break information technology down:
The iterator variable i
is initialized to 0. i
in this case refers to accessing the alphabetize of the assortment. This means that the loop will admission the first array value when it runs for the first time.
The conditioni < myNumbersArray.length
tells the loop when to stop, and the increment statement i++
tells how much to increment the iterator variable for each loop.
In other words, the loop starts at 0 index
, checks the length of the array, prints out the value to the screen, and so increases the variable by i. The loop prints out the contents of the array one at a time and when it reaches its length, it stops.
Conclusion
This article covered the basics on how to become started with for
loops in JavaScript. We learned how to loop through arrays using that method which is i of the most common ones you'll use when you're starting to learn the linguistic communication.
If you want to learn about other JavaScript array methods, y'all can read all near them hither.
Thanks for reading and happy coding!
Learn to code for free. freeCodeCamp's open up source curriculum has helped more than than forty,000 people get jobs as developers. Become started
Source: https://www.freecodecamp.org/news/javascript-for-loop-how-to-loop-through-an-array-in-js/
Post a Comment for "Js Why Can You Declare the Same Variable Again in a for Loop"