一.分支:主要有if语句和switch语句
二.循环:while循环,do-while循环,for循环
三.for in 循环
for in循环本质上是一种foreach循环,主要用来1)遍历数组里的所有数组元素,2)遍历javascript对象的所有属性。
遍历数组元素:
<script type="text/javascript">
var a = ['hello', 'javascript', 'yang'];
for (i in a)
document.write('a['+i+']'+'的值是'+a[i]+'<br/>');
</script>
输出:
a[0]的值是hello
a[1]的值是javascript
a[2]的值是yang
遍历对象属性:
(用for...in是无法遍历出javascript内置对象String/Array/Number/Datt/Function的方法和属性。暂无方法,只能看ECMAScript文档,才能知道。)
<script type="text/javascript">
for (i in navigator)
document.write('属性 '+i+'值为'+navigator[i]+'<br/>');
</script>
输出:
属性 geolocation值为[object Geolocation]
属性 webkitPersistentStorage值为[object StorageQuota]
属性 webkitTemporaryStorage值为[object StorageQuota]
属性 doNotTrack值为null
属性 onLine值为true
属性 product值为Gecko
属性 appCodeName值为Mozilla
属性 userAgent值为Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
属性 platform值为Win32
属性 appVersion值为5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
属性 appName值为Netscape
属性 vendorSub值为
属性 vendor值为Google Inc.
属性 productSub值为20030107
属性 cookieEnabled值为true
属性 mimeTypes值为[object MimeTypeArray]
属性 plugins值为[object PluginArray]
属性 language值为zh-CN
属性 javaEnabled值为function javaEnabled() { [native code] }
属性 getStorageUpdates值为function getStorageUpdates() { [native code] }
属性 registerProtocolHandler值为function registerProtocolHandler() { [native code] }
属性 webkitGetGamepads值为function webkitGetGamepads() { [native code] }
属性 webkitGetUserMedia值为function webkitGetUserMedia() { [native code] }
属性 vibrate值为function vibrate() { [native code] }
四.break和continue
break和continue都可用于终止循环,区别是continue只是终止本次循环,接着开始下一次循环,而break则是完全终止整个循环,开始执行循环后的代码。