Home > Web Front-end > JS Tutorial > Explain how to use the for...in statement in JavaScript_Basic knowledge

Explain how to use the for...in statement in JavaScript_Basic knowledge

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:56:44
Original
1470 people have browsed it

Here is another loop supported by JavaScript. It's called a for...in loop. This loop is used to loop through the properties of an object.

Using this loop may feel a bit unclear since we don’t have an object to discuss yet. However, once you understand JavaScript objects, you will find this loop very useful.
Grammar

for (variablename in object){
 statement or block to execute
}

Copy after login

Each iteration from the object assigns an attribute to the variable name (variablename), and this loop continues until all attributes of the object are exhausted.
Example:

The following is to print out the properties of the Navigator object of the web browser, as in the following example:

<script type="text/javascript">
<!--
var aProperty;
document.write("Navigator Object Properties<br /> ");
for (aProperty in navigator)
{
 document.write(aProperty);
 document.write("<br />");
}
document.write("Exiting from the loop!");
//-->
</script>

Copy after login

This will produce the following results:

Navigator Object Properties
appCodeName
appName
appMinorVersion
cpuClass
platform
plugins
opsProfile
userProfile
systemLanguage
userLanguage
appVersion
userAgent
onLine
cookieEnabled
mimeTypes
Exiting from the loop! 

Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template