While the terms Library and Framework may sound similar, they both work differently.
Many people use these two words interchangeably without knowing the profound meaning behind them.
Before we dig into the key differences between Library and Framework, let's look at the common purpose that they both serve.
Both Library and Framework are code written by some developer to solve a complicated problem efficiently.
They both give you an excellent approach to write DRY (don't repeat yourself) code.
Their purpose was to increase the reusability of the code so that you can use the same piece of code or functions again in your various project.
What is Library?
A Library is a set of code that was previously written by a developer that you can call when you are building your project.
In Library, you import or call specific methods that you need for your project.
In simple words, a bunch of code packed together that can be used repeatedly is known as Library.
Reusability is one of the main reasons to use libraries.
Let's understand this more clearly with the help of an example.
Think of you as a carpenter who needs to build a table.
Now, you can build a table without the help of tools, but it's time-consuming and a long process.
Whereas, if you choose the correct tools, you'll be able to build a table more quickly and that too without any hardship.
Think of here tools as a library. You can write your program without them.
But it will be a long process, and chances are your program will get buggy, while if you use Library, it'll be much easier for you to work with the program.
For example, if you use the in-built JavaScript fetch () method to fetch the data from API and you feel that it's not the ideal solution.
Then you can use Axios Library for the same purpose to make your work easier.
axios.post('/login', {
firstName: 'Monica',
lastName: 'robinson '
})
.then((response) => {
console.log(response);
}, (error) => {
console.log(error);
});
Some common examples of Library are:
React is a JavaScript library for building user interfaces.
Redux
Redux is an open-source JavaScript library for managing application state. It's most commonly used with React
Three.js
It's another super cool JavaScript library used to create and display 3d computer graphics.
Lodash
Lodash is a JavaScript library that provides utility functions for common programming tasks.
It's more of a productivity kit in node.js
jQuery
jQuery is a JavaScript library that does the things like event handling and HTML document manipulation.
What is Framework?
A framework is a supporting structure that gives shape to your code.
In the Framework, you have to fill the structure accordingly with your code.
There is a specific structure for a particular framework that you have to follow, and it's generally more restrictive than Library.
One thing to remember here is that frameworks sometimes get quite large, so they may also use the Library.
But the Framework doesn't necessarily have to use Library.
Let's get back to our carpenter and table example for a better understanding of the Framework.
Here, if you want to build a table, then you need a model or skeleton for how the table looks, like the table has four legs and a top slab.
Now, this is the core structure of the table and you have to work accordingly to build the table.
Similar to this, Framework also provides the structure, and you have to write the code accordingly.
Let's take the example of Express and understand the restrictive nature of the Framework.
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('welcome to hashnode!! ')
})
app.post('/', function (req, res) {
res.send('POST request to the hashnode homepage')
})
Here express is designed in such a way that it is going to look only for specific methods (get/post) and specific parameters.
You can't name the methods whatever you want to, and you have to name the methods as per the documentation.
Some common examples of Framework are:
Angular is a JavaScript framework for web and mobile development.
Django
Django is a fully featured server-side web framework written in Python.
Express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
Rails
Rails is a web application development framework written in the Ruby programming language.
Spring
Spring Framework is an open-source framework for building web applications with Java as a programming language.
Key Difference between Library vs Framework
The main key difference between the Library and Framework is something known as inversion of control.
Let's understand this inversion of control more in detail.
When you import a library, you have to call the specific methods or functions of your choice so, and it's up to you when and where to call the Library.
Here, you are in charge of flow.
On the other hand, Framework itself makes a call to your code and provide you with some space to write down details.
So, while using framework your framework is in charge of flow.
In Library, your code is going to call the Library whereas, in Framework, your code is being called by Framework.
Conclusion
I know it's getting too confusing but stay with me. I'll end this with one last crucial point.
Here is a simple thing to remember Framework is often more restrictive and generally have a more set of rules.
Whereas, Library is not bounded by many rules.
I hope you get a broader perspective of what's the difference between Library and Framework.
If you find my work interesting and worth reading you can appreciate me on Twitter and LinkedIn.
All kinds of Feedback are welcomed in comments.