What are Web3.js or Ethers.js? What is their purpose?
Okay, no problem. Let's talk about Web3.js and Ethers.js.
What are Web3.js and Ethers.js?
You can think of Web3.js and Ethers.js as a "bridge" or an "interpreter."
- One end of the bridge: Is the website or app you are using (e.g., a decentralized exchange, an NFT marketplace).
- The other end of the bridge: Is the Ethereum blockchain network.
The job of this "interpreter" is to translate your intentions (expressed through the website) into a language the Ethereum blockchain can understand. Then, it translates the results returned by the blockchain back into a format the website can display to you.
Essentially, both are JavaScript libraries, meaning they are toolkits programmers use to develop Web3 applications.
Why do we need them?
Imagine the Ethereum blockchain as a supercomputer that speaks an "alien language." It has its own set of communication rules (technically called the JSON-RPC
protocol), which are very low-level and complex.
On the other hand, the browsers we use for everyday internet browsing only understand "Earth languages" like JavaScript and HTML.
If you tried to make a website communicate directly with Ethereum, it would be like talking to a brick wall. This is where Web3.js or Ethers.js step in and say, "Don't worry, I'll be the translator!"
They encapsulate those complex "alien language" instructions into simple, easy-to-understand JavaScript functions. Programmers can then interact with the Ethereum network by simply calling these functions, significantly lowering the barrier to entry for development.
What exactly can they do? (Their functions)
With this "bridge," we can do many powerful things. Essentially, all operations that involve interacting with the Ethereum blockchain can be accomplished through them:
-
Connect to the Ethereum Network
- This is the first step, just like plugging an ethernet cable into your computer. They can connect to public Ethereum nodes or to your own wallet (e.g., MetaMask).
-
Query Information on the Blockchain (Read-only operations)
- Check balance: See how much Ether (ETH) or other tokens a specific wallet address holds.
- Check transactions: Get detailed information about a transaction, such as who sent what to whom and how much.
- Check block information: Retrieve the current latest block number or find out which transactions are included in a specific block.
-
Initiate Transactions (Write operations)
- Transfer funds: Send ETH from your account to someone else's.
- Interact with smart contracts: This is the most crucial functionality. For example, when you click the "Buy" button on an NFT marketplace, Web3.js/Ethers.js is behind the scenes calling the
buy
function of the NFT contract.
-
Deep Interaction with Smart Contracts
- You can imagine it as remotely controlling a mini-application deployed on the blockchain. You can read the state of this mini-application (e.g., your character's level in a game) or call its functions to change its state (e.g., execute an "upgrade" operation).
-
Wallet Functionalities
- Create new Ethereum wallet addresses.
- Sign messages to prove that an operation was indeed initiated by you (this is used when logging into many dApps).
Web3.js vs. Ethers.js: What are the differences?
This is a classic question, like asking "Which is better, KFC or McDonald's?" Both can help you order food (interact with Ethereum), but they offer different styles and experiences.
-
Web3.js: The veteran player, a pioneer.
- It was the first to emerge and was once actively promoted by the Ethereum Foundation.
- Many older projects and tutorials are based on it, and it has a very vast ecosystem.
- The downsides are its larger bundle size, and some of its designs can seem a bit outdated and complex by modern standards.
-
Ethers.js: The up-and-comer, more modern and lightweight.
- It was designed from scratch by a well-known Ethereum developer, with the goal of being smaller, simpler, and more aligned with modern JavaScript development practices.
- Its documentation is very clear, and the API is designed to be more intuitive (for example, it clearly distinguishes between
Provider
for read-only connections andSigner
for signing/write operations, making developers' thought processes clearer). - Smaller bundle size, more friendly to modern front-end development tools.
- Currently, Ethers.js is increasingly popular in new projects, and its community is very active.
A Simple Summary Analogy
If we liken the entire Ethereum ecosystem to a super banking system:
- The Ethereum blockchain: Is the bank's main vault, recording everyone's ledger, absolutely secure.
- Smart contracts: Are individual safety deposit boxes within the vault, each with its own unique deposit and withdrawal rules.
- Your wallet (MetaMask): Is your bank card + PIN, representing your identity and authorization.
- Web3.js or Ethers.js: Are the ATM machine or bank teller in front of you.
You wouldn't directly go into the vault to manipulate the ledger. Instead, you use the ATM/teller (Web3.js/Ethers.js) to check balances, transfer funds, or interact with your safety deposit boxes (smart contracts).
Which one should I learn?
If you are a beginner, I personally recommend starting with Ethers.js.
The reasons are simple: its design is more modern, its documentation is more beginner-friendly, and its bundle size is smaller. Learning it will give you a very clear understanding of how to interact with the blockchain.
Of course, Web3.js, as a "veteran," still has a large user base, and many companies' older projects still use it. So, once you are familiar with Ethers.js, it would also be beneficial to take some time to learn Web3.js's usage, so you can "be proficient in both."