Photo by Fotis Fotopoulos on Unsplash
What is the difference between a framework and a library in JavaScript? Why is this confusing?
It's sometimes hard to get a straight answer, but if we focus simply on the architectural design details the difference becomes quite clear.
Let's take a few minutes to answer this question. The topic is difficult to cover due to inconsistencies. Additionally, programmers use the two words "library" and "framework" interchangeably. This is where the misunderstanding is born. If we begin to delve into this topic, we may be more confused than before. People who yesterday used the term "library" for "framework" are using different terms today. There are many uncertainties here. I understand one difference.
The Solution
First of all, "libraries" and "frameworks" were created to solve common and recurring problems. They were written by developers to make it easier for everyone and the whole host of programmers to write.
The magic
All in all, there is nothing extraordinary and magical about both. Both the library and framework were written for multiple uses by someone else. Purpose - To facilitate the resolution of common problems.
Who defines these terms?
If one person had defined these two terms, there would not be so many discussions, misunderstandings and inconsistencies. Unfortunately, this is not the case. Both terms are and have been defined by the developer community. This makes the use of these words interchangeable and the treatment of them looser.
Common Problems
For example, very often in JavaScript, we create AJAX queries, manipulate the DOM. We don't want to write the same code now and then, do we? Same, identical code in 5 different projects? Why not create reusable code that will speed up your work at the same time?
The library
In the library, just like in life. You enter the library, you are looking for the book you want to read. The library allows you to choose the book YOU WANT. You are in control. Same in JS. Suppose you often need simple math functions.
function addTwo (a, b) {
return a + b;
}
function multiplyTwo (a, b) {
return a * b;
}
function substractTwo (a, b) {
return a - b;
}
Well done, you wrote a simple but your library!
Pros of the library
- We don't reinvent the wheel
- Provides reusable functions
- It allows you to choose the libraries you are interested in
- Reduces application development costs
- Eliminates the need to write code for complex functions
The framework
In the framework, just like at work. You go to her, you have specific tasks, you do not have full control. The framework is like building a house. You have a specific plan, budget, limited choices. You're sticking to the frame. The contractor is in control here. You can bring something when needed.
Pros of the framework
- Build applications faster with less code
- It is cross-platform
- Replacing in one part does not affect the entire application
Difference
I think you can already see the difference between the library and the framework. It's about control. The technical difference is in the concept called inversion of control.
If you use the library, you choose when and where to use it. You are responsible for the flow of the application.
If you use a framework, the framework is responsible for the flow. It gives you several places and options where you can enter your code. It gives you a framework within which you can move around. If you fill in the blanks, the framework will call and execute your code as needed.
Conclusion
Of course, libraries and frameworks also have their drawbacks. The framework is used to create and deploy applications faster. We must always be up to date with new versions and news. When choosing a framework, you need to delve into its ecosystem and understand it. The library extends and increases the functionality of the application. When creating your own library, you can use it many times.
Summary
Framework reverses control of the application. He tells the programmer what he needs.
You are in control of the library. You call it when, where and how many times you want.
Working with the library you have full freedom, and with the framework - you stick to the framework defined by it.