Home  >  Article  >  Web Front-end  >  What is the difference between foreach and for loop

What is the difference between foreach and for loop

青灯夜游
青灯夜游Original
2023-01-05 16:26:257793browse

Difference: 1. for loops through each data element through the index, while forEach loops through the data elements of the array through the JS underlying program; 2. for can terminate the execution of the loop through the break keyword, and forEach cannot; 3. for can control the execution of the loop by controlling the value of the loop variable, but forEach cannot; 4. for can call loop variables outside the loop, but forEach cannot call loop variables outside the loop; 5. Execution of for The efficiency is higher than forEach.

What is the difference between foreach and for loop

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

1. The difference between for loop and forEach

(1) The for loop loops through each data element of the array by generating the index subscript of the array

What is the difference between foreach and for loop

forEach is a function method of an array defined by JavaScript that loops through the data elements of the array through the JavaScript underlying program

What is the difference between foreach and for loop

#(2) The for loop can terminate the execution of the loop through the break keyword

What is the difference between foreach and for loop

Call in forEach When executing the break program, an error will be reported

What is the difference between foreach and for loop

(3) The for loop can control the execution of the loop by controlling the value of the loop variable

What is the difference between foreach and for loop

The parameter storage data in the forEach loop is assigned by the underlying program and cannot be assigned or modified.

What is the difference between foreach and for loop

(4) The for loop can call loop variables outside the loop

What is the difference between foreach and for loop

forEach loop cannot call loop variables outside the loop

What is the difference between foreach and for loop
##(5) The execution efficiency of for loop is higher than that of forEach loop

2. Comparison of for loop and forEach loop

Since the syntax of the for loop and forEach loop looks like The for loop is more convenient and more reasonable. Why are there two loops, for loop and forEach?

First of all, the execution of the for loop can only generate the index subscript value through the loop and then operate the data elements of the array through the index subscript.

But forEach can store the index subscript data value by setting parameters, which is more convenient in operation

What is the difference between foreach and for loop# #Secondly, when the for loop generates and defines loop variables, you can use keywords such as var or let, so that the keywords for declaring variables can be set according to the needs of loop execution.

But the bottom layer of forEach uses the let keyword to declare variables and can only This keyword declares that variables cannot be modified

What is the difference between foreach and for loopIn actual projects, we can judge what we need to use based on the actual needs of our own projects. Is it a for loop or a forEach loop. Well, this article is shared here.

If you want to get Web front-end learning materials and source code, please click on the link below with your mobile phone to get a massive information package!

##

The above is the detailed content of What is the difference between foreach and for loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn