Definitions and Usage

The forEach() method is used to call each element of the array and pass the element to the callback function.

Note that forEach() does not perform callbacks on empty arrays.

grammar

array.forEach(function(currentValue, index, arr), thisValue)
Copy the code

The instance

<! DOCTYPE html> <html> <head> <meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
			[1, 2, 3, 4, 5].forEach(function(a) {
				console.log(a, this)
			})
		</script>
		<script type="text/javascript">
			[1, 2, 3, 4, 5].forEach(function(a) {
				console.log(a, this)
			}, [6, 7, 8, 9])
		</script>
		<script type="text/javascript">
			Array.prototype.forEach.call([1, 2, 3, 4, 5], function(a) {
				console.log(a, this)
			}, [6, 7, 8, 9])
		</script>
		<script type="text/javascript">
			Array.prototype.forEach.apply([1, 2, 3, 4, 5], [function(a) {
					console.log(a, this)
				},
				[6, 7, 8, 9]
			])
		</script>
		<script type="text/javascript">
			Array.prototype.forEach.bind([1, 2, 3, 4, 5])(function(a) {
				console.log(a, this)
			}, [6, 7, 8, 9])
		</script>
	</head>

	<body>
	</body>

</html>
Copy the code