TypeScript

Web3.js: A Casual Developer's Guide

Web3.js is a JavaScript library that lets you talk to the Ethereum blockchain. You can send transactions, deploy smart contracts, and read on-chain data —...

5 Mar 2024

Web3.js: A Casual Developer's Guide

Web3.js is a JavaScript library that lets you talk to the Ethereum blockchain. You can send transactions, deploy smart contracts, and read on-chain data — all from your JavaScript or TypeScript app.

Think of it as the HTTP client for Ethereum. Instead of hitting REST endpoints, you're calling smart contract functions and querying blocks.

Getting started

Install it:

Bash
npm install web3

Connecting to Ethereum

You need a node provider to connect to the network. Infura, Alchemy, and QuickNode are popular choices. Grab a project ID and initialize Web3:

Typescript
const Web3 = require('web3');
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');

You're now connected to Ethereum mainnet.

Reading data

Check an account balance:

Typescript
const balance = await web3.eth.getBalance('0x742d35Cc6634C0532925a3b844Bc9e7595f2bD18');
console.log(web3.utils.fromWei(balance, 'ether'));

Interacting with a smart contract

You need two things: the contract's ABI (its interface definition) and its deployed address.

Typescript
const contract = new web3.eth.Contract(ABI, contractAddress);
const result = await contract.methods.someFunction().call();

To write data (send a transaction), you also need a wallet with ETH for gas fees.

The trade-off

Web3.js gives you direct blockchain access from JavaScript. That's powerful.

But blockchain development has real friction. Transactions cost gas. Confirmations take time. Testnets behave differently from mainnet. And debugging failed transactions is painful — error messages from the EVM are cryptic at best.

Ethers.js is a lighter alternative to Web3.js with a cleaner API. If you're starting fresh, evaluate both before committing.

The blockchain ecosystem moves fast. Libraries, patterns, and best practices change quarter to quarter. Whatever you build, keep your dependencies up to date.