Like any other library, Web3.js is just trying to make things simple for you, the app developer.
You do not need to use it. In fact, you could literally rewrite all the work it does in your own code, but that would be an incredible waste of time.
Instead, Web3.js enables app developers to build things quickly and simply.
For example, get the balance of an Ethereum Address in 40 lines:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="./web3.min.js"></script>
<script type="text/javascript">
window.addEventListener('load', function () {
if (typeof web3 !== 'undefined') {
console.log('Web3 Detected! ' + web3.currentProvider.constructor.name)
window.web3 = new Web3(web3.currentProvider);
} else {
console.log('No Web3 Detected... using HTTP Provider')
window.web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/<APIKEY>"));
}
})
function getBalance() {
var address, wei, balance
address = document.getElementById("address").value
try {
web3.eth.getBalance(address, function (error, wei) {
if (!error) {
var balance = web3.fromWei(wei, 'ether');
document.getElementById("output").innerHTML = balance + " ETH";
}
});
} catch (err) {
document.getElementById("output").innerHTML = err;
}
}
</script>
</head>
<body>
<h1>ETH Balance Fetcher</h1>
<p>Enter your Ethereum Address:</p>
<input type="text" size="50" id="address" />
<button type="button" onClick="getBalance();">Get Balance</button>
<br />
<br />
<div id="output"></div>
</body>
</html>
Imagine having to implement:
- Connecting to a Web3 provider
- Integrating the
getBalance
function
- Converting from Wei to Ether
- etc...