If you look at the function below, on line 11 where it alert(template);
. It prints undefined
. If I alert(template);
inside the ajax succes
The success
function is a callback. The javascript engine makes the request to the server, and when the server responds to the data, then it runs the success
function and you have the data, so the success
function is run after your if statement at the end, even though its defined beforehand.
Javascript code is run inside of an Event Loop, all $.ajax
is doing is starting an asynchronous event and specifying what to do when that event occurs.