Example Analysis of JS for... in traversal statement usage, for... in instance analysis

Source: Internet
Author: User

Example Analysis of JS for... in traversal statement usage, for... in instance analysis

This article describes the usage of JS for... in traversal statements. We will share this with you for your reference. The details are as follows:

The for... in statement is used to perform cyclic operations on the attributes of an array or object.

For (variable in object)
{
Run the code here
}

The "variable" is used to specify the variable. The specified variable can be an array element or an object attribute.

For example:

<! DOCTYPE html> <meta charset = "UTF-8"> <script> var x; var zoon = new Array (); zoon [0] = "cat "; zoon [1] = "dog"; zoon [2] = "pig"; for (x in zoon) {console. log (zoon [x]);} var student = {}; student. name = "James"; student ["age"] = 20; for (var I in student) // The previous variable I is the property name of the object {console. log (I); // name age console. log (student [I]); // Zhang San 20} </script>

Run the following command:

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.