# web3.js — Ethereum JavaScript API for DApp Development > web3.js is a collection of TypeScript libraries that let JavaScript and TypeScript applications interact with Ethereum nodes via JSON-RPC. It handles account management, contract deployment, transaction signing, event subscriptions, and ABI encoding. web3.js is one of the foundational libraries of the Ethereum DApp ecosystem. ## Install Save as a script file and run: # web3.js — Ethereum JavaScript API for DApp Development ## Quick Use ```bash npm install web3 # In your script # const { Web3 } = require('web3'); # const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_KEY'); # const balance = await web3.eth.getBalance('0x...'); ``` ## Introduction web3.js is one of the original JavaScript libraries for interacting with the Ethereum blockchain. Created in 2015, it provides a comprehensive API for reading chain state, sending transactions, deploying contracts, and subscribing to events. It remains widely referenced in tutorials and production DApps. ## What web3.js Does - Connects to Ethereum nodes via HTTP, WebSocket, or IPC providers - Encodes and decodes contract calls using ABI definitions - Signs and sends transactions from local or injected wallets - Subscribes to real-time events such as new blocks, pending transactions, and log filters - Provides utility functions for unit conversion, hashing, and address checksum validation ## Architecture Overview web3.js v4 is a modular TypeScript rewrite organized into packages: `web3-eth` for core chain interaction, `web3-eth-contract` for smart contract abstraction, `web3-eth-accounts` for signing, `web3-utils` for helpers, and `web3-net` for network info. A central `Web3` class ties them together and manages the provider connection. Plugins extend functionality without modifying the core. ## Self-Hosting & Configuration - Install with `npm install web3` or import individual sub-packages for smaller bundles - Set the provider to a public RPC endpoint (Infura, Alchemy) or your own Geth/Reth node - Configure gas price strategies using `web3.eth.calculateFeeData()` for EIP-1559 transactions - Use `web3.eth.accounts.wallet` to manage local signing keys - Enable WebSocket provider for real-time subscriptions (`new Web3.providers.WebsocketProvider(url)`) ## Key Features - Plugin system allows community extensions without forking the core library - First-class TypeScript support with full type inference on contract methods - ENS (Ethereum Name Service) resolution built in - Supports batch JSON-RPC requests for reducing network round trips - Compatible with browser, Node.js, and React Native environments ## Comparison with Similar Tools - **ethers.js** — Lighter API with separate provider/signer pattern; web3.js uses a unified Web3 object - **Viem** — Modern TypeScript-first alternative with tree-shaking; web3.js has broader legacy ecosystem support - **Wagmi** — React hooks layer built on Viem; web3.js is framework-agnostic - **Alchemy SDK** — Wraps ethers.js with managed services; web3.js is provider-independent ## FAQ **Q: Is web3.js still maintained?** A: The 4.x branch received active development through ChainSafe. The repository is now archived, but the library remains functional and widely used. **Q: Should I use web3.js or ethers.js for a new project?** A: For new projects, ethers.js or Viem are generally recommended for their smaller bundle sizes and modern APIs. web3.js is a solid choice if your team already uses it or you rely on its plugin ecosystem. **Q: Does web3.js work with non-Ethereum chains?** A: Yes. It works with any EVM-compatible chain (Polygon, BSC, Avalanche) by changing the provider URL. **Q: How do I handle contract events?** A: Use `contract.events.EventName()` with a WebSocket provider to receive real-time event logs. ## Sources - https://github.com/web3/web3.js - https://docs.web3js.org --- Source: https://tokrepo.com/en/workflows/asset-145b41ca Author: Script Depot