If you have done any serious desktop or server-side development, you will have encountered either the Model-View-Controller (MVC) design pattern or its derivative Model-View-View-Model (MVVM). I am not going to describe either pattern in any detail, other than to say that the core concept in both is separating the data, operations, and presentation of an application into separate components.
There is a lot of benefit in applying the same basic principles to a web application. I am not going to get bogged down in the design patterns and terminology. Instead, I am going to focus on demonstrating the process for structuring a web app and explaining the benefits that are gained from doing so.