The difference between foreach,for in,for in JS

Source: Internet
Author: User

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <Metaname= "Viewport"content= "Width=device-width, User-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">    <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">    <title>Document</title></Head><Body><Script>    vararr= [1,2,3]; Arr.four= 4;    Console.log (arr); Arr.foreach (Value= {        /*if (value > 2) {break}*/Console.log (value)// the    })     for(Let Iincharr) {Console.log (i);//' 0 ' 1 ' 2 ' four ' equals the object's key valueConsole.log (Arr[i])//1,2,3,4 equivalent to the value of the object    }     for(Let I of arr) {if(i> 2) {             Break} console.log (i); //The value of each item in a number ofConsole.log (Arr[i]);//2,3,undefined equivalent to arr[1],arr[2],arr[3]    }     for(Let I of'my') {console.log (i);//' m ' Y '    }     for(Let I of arr) {if(i> 2) {             Break} console.log (i); // the    }</Script></Body></HTML>

Summary: If traversing an array is best used for the, traversing the object with the for in
The difference between the for of traversal array and the foreach Traversal array: For of can use the break statement, foreach cannot

The difference between foreach,for in,for in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.