Creating a web3 object

if (typeof web3 !== 'undefined') 
{ 
web3 = new Web3(web3.currentProvider); 
} 
else 
{ 
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost: 8545")); 
} 

This code checks whether there is already an available provider; if yes, then it will set the provider to the current provider. Otherwise, it sets the web3 provider to localhost: 8001; this is where the local instance of geth is running.

Get Mastering Blockchain - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.