The difference between for and for all in JavaScript

Source: Internet
Author: User

the difference between for and for all in JavaScript

Each of the two functions is used to traverse the object, but why is there a for in statement and a for every in statement, and then look at the documentation that is being developed for every in, which is published as part of the E4X standard in JavaScript 1.6. And it's not part of the ECMAScript standard.

Difference One:

For in is published in JavaScript 1.0.

For all in is published as part of the E4X standard in JavaScript 1.6, and it is not part of the ECMAScript standard.

This would mean a variety of browser compatibility issues. For every in, not supported by many browsers. For example, browsers such as IE6,IE7,IE8 are not supported.

Difference Two:

Example: var rectangle = {High: "15", Width: "25"};

?

1 2 3 for (var i in rectangle) {alert (i + "," + rectangular [i]);}

The results are: high, 15; Wide, 25;

?

1 2 3 For each (var i in rectangle) {alert (i + "," + rectangular [i]);}

The results are as follows: undefined; undefined;

The value of the variable I of the two traversal methods is not the same, for every in cannot get the property name of the object, only the property value can be obtained.

Finally summed up the use of recommendations:

(1) traverse the normal array, it is recommended that you use the native traversal method for, and do not covet convenience, because there are browser compatibility issues in both for and for, because there is no guarantee of their array traversal order (if the order is not required, you can use for in, but I do not recommend), If you are interested, you can read the next article "about JS in the defect analysis."

(2) Traversal of the object, because there is no way to provide the ideal traversal, so you can only select other methods. It is recommended here to use for-in, which is explained above, for-in is more advantageous than for, for-in to get index and property values, while for each can only get property values, and for each is not supported in many low versions of browsers.

The above mentioned is the entire content of this article, I hope you can enjoy

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.