Getting Started With Web3 JS
An overview of the most useful and commonly used functions

Introduction
Web3 bridges the gap between the traditional internet and the Ethereum Blockchain. It enables users to interact with your DApp through a browser. When using Javascript for your front end, knowing the ins and outs of Web3JS is essential.
Here is a list of some of the most useful and commonly used functions in Web3JS.
web3.eth.getAccounts()
Use this function to get all of the available account addresses.
Usage:
let accounts = await web3.eth.getAccounts();
console.log(accounts[0]);or
web3.eth.getAccounts().then(console.log);web3.eth.sendTransaction()
Use this to send Ether from an account to another, or a Smart Contract address. It requires a few parameters depending on the transaction. Possible parameters include: from, to, value, gas, and more.
If you’re using this method as part of your front end, you can catch certain events which occur during submission to the Blockchain. These events are transactionHash, receipt, confirmation and error.
Usage:
web3.eth.sendTransaction({
from: account1,
to: account2,
value: 1000000000
})
.on('transactionHash', () => {
...
})
.on('receipt', () => {
...
})
.on('confirmation', () => {
...
})
.on('error', () => {
...
})web3.eth.estimateGas()
If you’re sending a transaction to a contract, you might have to estimate the gas. Pass the same parameters as web3.eth.sendTransaction() to receive the gas estimate. You can then add the result to the parameters in your web3.eth.sendTransaction() call.
Usage:
let gasEstimate = await web3.eth.estimateGas({
from: account1,
to: account2,
value: 1000000000
});web3.eth.sendTransaction({
from: account1,
to: account2,
value: 1000000000,
gas: gasEstimate
})
.on('transactionHash', () => {
...new web3.eth.Contract()
Use this function to load your deployed contract into your DApp so you can start interacting with it. Parameters include the ABI and the address the contract was deployed to.
Usage:
let contractInstance = new web3.eth.Contract(
MyContract.abi,
deployedAddress
);
await contractInstance.doSomething();web3.utils.toWei()
Use this function to convert Ether values into Wei, the unit of value used by Smart Contracts.
Usage:
let weiValue = web3.utils.toWei("1", "ether");
...Further Reading
Dive into the Web3JS documentation to learn about these, and more.
Browse a collection of tutorials, walkthroughs, explanations, and cheat sheets to build your experience with blockchain development.
