I don't really understand that part of forEach loop

let fruits = [“Apple”,“Mango”,“Orange”,“StrawBerry”,“BlackBerry”];

let text ="<ul>";

Its working above from its function !! Why???
 fruits.forEach(myfunction)

text +="<ul>";
document.getElementById(“demo”).innerHTML=text;

function myfunction(value){

text = text +"<li>"+value+"</li>";
}

The forEach is not working here !!
fruits.forEach(myfunction)

if you do text = ... you are overwriting the value of text
if you do text += ... you are adding at the end of text

note which one of the two is used in the two cases

if i remove one of the case from both … then the result will not display … i don’t know why

wait, are you asking why you can use myFunction before declaring it?

it’s a feature of that way of creating functions

yes !! the forEach is declared above and i don’t understand the concept
because I’ve mostly perform forEach like this:

let array =[1,2,3,4,5,6];
function myFun(values){
console.log(All the values in array${values});
}
array.forEach(myFun)

sorry for my previous confusing answer

this is a thing with function declarations, you can find it for example here:

1 Like

Thank you ieahleen :slight_smile:

You can also find some hints reading about Hoisting, if I understand your question.

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.

extracted from scotch.io

The consequence is that you can access the those functions anywhere in the doc.