Advertisemen
If you are reading this, I believe, you know already what loop is. If you are a PHP developer, you might find some of the behaviours of FOR loop in javascript are different.
First way to use FOR Loop :
This is pretty much same in PHP and other programming languages.
<div id="displayFruits"></div>Second way to use FOR Loop :
<script>
var fruits = ["Mango", "Banana", "Apple", "Pineapple"];
for(var i = 0; i < fruits.length; i++)
{
document.getElementById("displayFruits")
.innerHTML += fruits[i]+'<br>';
}
</script>
This one is totally different than any other languages.
<div id="displayFruits"></div>Third way to use FOR Loop :
<script>
var fruits = ["Mango", "Banana", "Apple", "Pineapple"];
fruits.forEach(function (fruit)
{
document.getElementById("displayFruits")
.innerHTML += fruit+'<br>';
});
</script>
This one is little bit different than other languages.
<div id="displayFruits"></div>
<script>
var fruits = ["Mango", "Banana", "Apple", "Pineapple"];
for(var fruit in fruits)
{
document.getElementById("displayFruits")
.innerHTML += fruits[fruit]+'<br>';
}
</script>
Result :
Mango
Banana
Apple
Pineapple
Advertisemen
Tidak ada komentar:
Posting Komentar