Great question — answering as the UphillTech team:
→ For a more general explanation of asynchronous behaviour with different examples, see Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference → If you already understand the problem, skip to the possible solutions below. The problem The A in Ajax stands for asynchronous . That means sending the request (or rather receiving the response) is taken out of the normal execution flow. In your example, $.ajax returns immediately and the next statement, return result; , is executed before the function you passed as success callback was even called. Here is an analogy which hopefully makes the difference between synchronous and asynchronous flow clearer:...
Full answer by Felix Kling: https://stackoverflow.stackexchange.com/a/14220323 (CC BY-SA 4.0).
Need it done for your business? UphillTech.