What is Web3.js and what is it used for?

Answer

Web3.js is an Ethereum JavaScript library that enables web applications to interact with the Ethereum network. It's the original Web3 library (released 2015). Key capabilities: (1) Connect to Ethereum nodes — via HTTP, WebSocket, or IPC providers (MetaMask, Infura, Alchemy, local node); (2) Query blockchain data — get block data, transaction receipts, account balances; (3) Interact with smart contracts — call view functions and send state-changing transactions; (4) Manage accounts — create accounts, sign transactions; (5) Listen to events — subscribe to contract events and new block headers. Example: const Web3 = require("web3"); const web3 = new Web3("https://mainnet.infura.io/v3/YOUR_KEY"); const balance = await web3.eth.getBalance("0x..."); const contract = new web3.eth.Contract(abi, address); const result = await contract.methods.balanceOf(userAddress).call(). Web3.js has largely been superseded by ethers.js for new projects — ethers.js has a cleaner API, better TypeScript support, and smaller bundle size. Wagmi and viem are modern alternatives for React applications.