Javascript Loops and Arrays
One critical piece of programming is learning to work with loops and arrays or other sets of data.
Loops in programming allow you to repeat a block of code multiple times. This is useful when you have a set of actions that you want to perform on a collection of data, such as an array.
In JavaScript, there are two main types of loops: for
loops and while
loops.
A for
loop has three parts:
- Initialization: A starting value for the loop counter, which is usually a variable.
- Condition: A statement that determines when the loop should continue running.
- Increment: An operation that updates the loop counter after each iteration of the loop.
Here is an example of a for
loop in JavaScript:
|
|
This loop will run 10 times, printing the values 0 through 9 to the console. On each iteration, the loop counter i
is incremented by 1, and the loop continues running until i
is no longer less than 10.
A while
loop, on the other hand, only has a condition. It continues running as long as the condition is true:
|
|
This loop will also run 10 times, but the loop counter i
is updated inside the loop instead of in the loop definition.
In both cases, the code inside the loop is executed multiple times, allowing you to perform a set of actions on a collection of data. Loops are a fundamental tool in programming, and they make it possible to automate repetitive tasks and process data efficiently.
# Advanced Loop Concepts
# Break and Continue
break
statement: This statement allows you to exit a loop prematurely. The loop stops executing and control is transferred to the next statement following the loop.
|
|
continue
statement: This statement allows you to skip over the current iteration of a loop and move on to the next iteration.
|
|
Note: Both
break
andcontinue
statements can be used withfor
andwhile
loops in JavaScript.
here’s an example of a while
loop using both break
and continue
statements:
|
|
In this example, the loop continues to execute as long as i
is less than 10. If i
is equal to 5, the break
statement is triggered and the loop terminates. If i
is even, the continue
statement is triggered and the current iteration is skipped, moving on to the next iteration of the loop.
# Extra Loop Syntax
there are a few other syntax for loops in JavaScript beyond break
and continue
.
label
: You can add a label to a loop, and use that label in the break
or continue
statement to specify which loop to exit or continue.
|
|
In this example, the break outer
statement breaks out of both loops and terminates the execution.
for...in
loop: This type of loop is used to iterate over the properties of an object.
|
|
for...of
loop: This type of loop is used to iterate over the values of an iterable object, such as arrays, strings, and Map/Set objects.
|
|
The
forEach
method does not have a built-in way tobreak
out of the loop orcontinue
to the next iteration. However, you can usereturn
to exit theforEach
callback function early.
# More Examples
- Print out each number in array
|
|
- Filter, and Sort array of json objects
|
|
- Printing numbers from 1 to 10:
|
|
- Calculating the sum of an array of numbers:
|
|
- Finding the largest number in an array:
|
|
- Reversing the elements of an array:
|
|