Web3.js 🌐⛓️

Connect Web Apps to Blockchain

1. What is Web3.js?

Web3.js is a JavaScript library that allows web applications to interact with the Ethereum blockchain.

2. Why Web3.js?

Frontend (React / HTML) → Web3.js → Ethereum Blockchain

3. Key Features

Send transactions
Read blockchain data
Interact with smart contracts
Connect wallets (MetaMask)

4. Installation

npm install web3

5. Connect to Ethereum

import Web3 from "web3";

const web3 = new Web3(window.ethereum);
await window.ethereum.request({ method: "eth_requestAccounts" });

6. Read Blockchain Data

const balance = await web3.eth.getBalance(address);
console.log(balance);

7. Send Transaction

web3.eth.sendTransaction({
  from: account,
  to: receiver,
  value: web3.utils.toWei("0.01", "ether")
});

8. Smart Contract Interaction

contract.methods.setData(10).send({ from: account });

contract.methods.getData().call();

9. Use Cases