Difference between the forin loop of js and the foreach loop in java

Source: Internet
Author: User
This article mainly introduces the differences between the forin loop of js and the foreach loop in java. The example analyzes the use skills of the forin loop of js and explains the differences between the use of the foreach loop and that of Java, it has some reference value. If you need it, you can refer to the examples in this article to analyze the differences between the js for in loop and the foreach loop in java. Share it with you for your reference. The specific analysis is as follows:

The for in loop in js is defined as follows:

for(var variable in obj) { ... }

Obj can be a common js object or an array. If obj is a js object, variable obtains the property name of the object in the traversal, rather than the value corresponding to the property. If obj is an array, variable returns the subscript of the array during traversal.

Traverse object experiment:

var v = {};  v.field1 = "a";  v.field2 = "b";  for(var v in v) {      console.log(v);  }

Console output:


Field1
Field2

Traverse the array experiment:

var mycars = new Array()mycars[0] = "Saab"mycars[1] = "Volvo"mycars[2] = "BMW"  for (var x in mycars){  console.log(x);}

Console output:


0
1
2

There are two major differences between java's foreach loop. First, the java foreach loop does not enumerate the attributes of a java object. Second, when java's foreach cyclically enumerates an array or any Object implementing the Iterable interface, for (Object o: list), Object o gets a list element, instead of the subscript in the list.

Java traversal code will not be pasted out. I often write background code, and I am familiar with foreach loops. When writing front-end js code, it is inevitable that java syntax will be applied, so the first time JavaScript's for in loop is used, it is wrong. The conclusion is clear, and no mistakes will be made in the future.

I hope this article will help you design javascript programs.

For more information about the differences between js for in loops and foreach loops in java, see the PHP Chinese website!

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.