First read the code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>javascript</title>
</head>
<body>
<p id="demo"></p>
<script>
let friends= ["aneeq","yasaal","ayaz","shahzaib","areeb"]
friends.forEach(fun);
function fun(item, index) {
console.log(index + 1 + ":" +item);
document.getElementById('demo').innerHTML += index + ":" + item;
}
</script>
</body>
</html>
And its output is
0:apple
1:orange
2:cherry
And I am confused that why the use of +=
between .innerHTML
and index
in
document.getElementById("demo").innerHTML += index + ":" + item;
is must because if i write it like this
document.getElementById("demo").innerHTML = index + ":" + item;
the output i will get is:
2:cherry
It will not return every item in array why?
Aucun commentaire:
Enregistrer un commentaire