Marketplace has launched, further enabling blockchain developers! Learn more

How to generate a new Ethereum address in JavaScript

September 09, 2022

Overview

When it comes to programming, there’s hardly anyone who has not used or heard about JavaScript. JavaScript was initially created for client-side scripting but has become a full-featured Object-Oriented and procedural language widely used for client and server applications today. JavaScript’s syntax borrows from Java and C++, and it is based on the ECMAScript specification. With over 1,444,231 libraries and counting, JavaScript today is practically used everywhere, including making dApps (Decentralized Applications) on Ethereum. In this guide, we will cover creating an Ethereum address in JavaScript using ethers.js.

Prerequisites
  • NodeJS installed on your system.
  • A text editor
  • Terminal aka Command Line

What is an Ethereum address?

While signing in to any platform on the internet, you need to authenticate using a combination of credentials. Consider an Ethereum address as your username and a corresponding private key as the password. While your Ethereum address is public and can be shared, the private key must always be kept secret. Using this combination lets you interact with the Ethereum blockchain. An Ethereum address is your identity on the blockchain, and it looks like this “0x6E0d01A76C3Cf4288372a29124A26D4353EE51BE”. Having a valid Ethereum address is required for:

  • Receiving/Sending Ethereum currency
  • Signing/Sending transactions
  • Connecting to decentralized applications

How an Ethereum address is generated:

  • A random private key of 64 (hex) characters (256 bits / 32 bytes) is generated first. 
    For example: 

how an ethereum address is generated

Copy
0xf4a2b939592564feb35ab10a8e04f6f2fe0943579fb3c9c33505298978b74893

  •  A 128 (hex) character (64 bytes) public key is then derived from the generated private key using Elliptic Curve Digital Signature Algorithm (ECDSA). 
    For example: 

how an ethereum address is generated

Copy
0x04345f1a86ebf24a6dbeff80f6a2a574d46efaa3ad3988de94aa68b695f09db9ddca37439f99548da0a1fe4acf4721a945a599a5d789c18a06b20349e803fdbbe3

  • The Keccak-256 hash function is then applied to (128 characters / 64 bytes) the public key to obtain a 64 character (32 bytes) hash string. The last 40 characters / 20 bytes of this string prefixed with 0x become the final Ethereum address. 
    For example:

how an ethereum address is generated

Copy
0xd5e099c71b797516c10ed0f0d895f429c2781142

Note: 0x in coding indicates that the number/string is written in hex.

What is ethers.js?

ethers.js is a lightweight alternative to Web3.js, which is the most commonly used Ethereum library today. Ethers.js is considered by some to be more stable and less buggy than other libraries and has extensive documentation. This library is also very friendly to beginners. Ethers.js is very well maintained and is preferred over Web3.js by many new developers.

You can learn more about ethers.js and How to connect to Ethereum network with ethers.js here.

Generating an Ethereum address in JavaScript

Our first step here will be to check if node.js is installed on the system. To do so, copy-paste the following in your terminal/cmd:

generating an ethereum address in javascript

Copy
$ node -v

If not installed, you can download the LTS version of NodeJS from the official website.

If node.js is properly installed, let’s add the ethers.js library using npm (Node Package Manager, a part of node.js).

generating an ethereum address in javascript

Copy
$ npm i ethers

The most common issue at this step is an internal failure with `node-gyp.` You can follow node-gyp installation instructions here.

Note: You will need to have your python version match one of the compatible versions listed in the instructions above if you encounter the node-gyp issue. 

Another common issue is a stale cache; clear your npm cache by entering the following command:

generating an ethereum address in javascript

Copy
$ npm cache clean

If ethers.js is successfully installed, let’s proceed with creating an Ethereum address.

Create a file named address.js, which will be a short script to create a random private key and an Ethereum address from that key, copy-paste the following in your address.js file:

generating an ethereum address in javascript

Copy
var ethers = require('ethers');  
var crypto = require('crypto');

var id = crypto.randomBytes(32).toString('hex');
var privateKey = "0x"+id;
console.log("SAVE BUT DO NOT SHARE THIS:", privateKey);

var wallet = new ethers.Wallet(privateKey);
console.log("Address: " + wallet.address);

Explanation of the code above

Line 1-2: Importing ethers library and crypto module that comes with node.js

Line 4: Generating a random 32 bytes hexadecimal string using the crypto object and storing it in the id variable.

Line 4: Adding ‘0x’ prefix to the string in id and storing the new string in a variable called privateKey.

Line 6: Printing our private key with a warning.

Line 8: Creating a new wallet using the privateKey and storing it in the wallet variable.

Line 9: Printing the address of the newly created wallet with a message “Address:”

Save and run your script to generate a new Ethereum address.

generating an ethereum address in javascript

Copy
$ node address

If your code executes successfully, the output will look similar to the screenshot below. The first line consists of the private key, and the second line consists of your new Ethereum address.



Conclusion

Your Ethereum address is your identity on the Ethereum network. It is required to interact with the network and perform transactions. To continue learning ethers.js, check out this guide on How to send an Ethereum transaction using ethers.js. Get more information on ethers.js from their official documentation. As you saw, generating a new Ethereum address is quickly done with JavaScript and the latest libraries. The development of dApps on the Ethereum blockchain is supported by a variety of tools that are continuously updated and improved by the fast-growing Ethereum community. Look out for more easy-to-follow guides from QuikNode - your provider of affordable and lightning-fast Ethereum nodes. Learn how to get started with QuikNode and spin up your own Ethereum node in minutes.

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet :)

Related articles 35

How to do a non-custodial transaction with QuickNode
Originally Published On: Jun 19, 2021
Updated On: Sep 9, 2022

Private keys are one of the most... A non-custodial wallet is a... Custodian of Private... At We could use pretty much any... We could use any of the many... Let us create a token transfer... Congratulations on making a...

Continue reading
How to Set Up a Near Project from Scratch
Originally Published On: Jan 27, 2022
Updated On: Sep 9, 2022

In this tutorial we will look at... In this section, we will... In this section, we will look at... In this section, we will look at... When writing smart contracts on... In the last section, we looked... In the last section, we looked... In the last section, we looked... We have finally come to the end...

Continue reading
Estimating gas price using pending transactions in Python
Originally Published On: Feb 20, 2021
Updated On: Sep 9, 2022

To send a transaction on the... Gas is the unit to measure the... Why do we want to estimate gas?... Our first step here would be to... For our purposes today, we could... The first thing we’re going to... Now you know how to estimate gas...

Continue reading
Como crear y lanzar un ERC-721 (NFT)
Originally Published On: Dec 27, 2021
Updated On: Sep 9, 2022

Coleccionables digitales que son... Fungible significa que es lo... ERC es la abreviación para... Ahora que conocemos qué es y... Antes de escribir nuestro... Felicidades por crear tu propio...

Continue reading
How to Access Bitcoin Mempool
Originally Published On: Aug 16, 2021
Updated On: Sep 21, 2022

Bitcoin is the father of... The transactions which are sent... For our purpose today, we need a... We will use Bitcoin RPC methods... Congratulations on mastering the...

Continue reading
How to Mint an NFT on Polygon with Ethers.js
Originally Published On: Mar 28, 2022
Updated On: Sep 9, 2022

This guide demonstrates how to... Ethers.js is a complete Ethereum... As a reminder, the act of... We will deploy our contract on... Time to write the code! Start by... Note, you will need some... That’s it! You have minted an...

Continue reading
How to access Ethereum Mempool
Originally Published On: Oct 15, 2020
Updated On: Sep 15, 2022

On Ethereum, when a transaction... In blockchain terminology, a... For the purpose of this guide,... First, query the Let’s examine how to subscribe... This short guide showed you how...

Continue reading
The Web3 Developer Stack
Originally Published On: Jul 3, 2021
Updated On: Sep 15, 2022

A developer stack is a bag of... If we can imagine web3 as a car... The Chainlink price feed... Head over to the We’ll need a Kovan node to... We’ll now write a short... Congratulations :D You’ve taken...

Continue reading
How to Mint NFTs on the Stacks Blockchain
Originally Published On: Aug 19, 2022
Updated On: Sep 9, 2022

Stacks is an emerging blockchain... The Stacks ecosystem has adopted... Open up a terminal window and... Implementing the SIP-009... This section will demonstrate... The moment you've been waiting... Awesome Work! You’ve learned how...

Continue reading
Introduction to Scaffold-ETH 🏗
Originally Published On: Oct 7, 2021
Updated On: Sep 9, 2022

Developing applications involves... First of all, before getting... Scaffold-ETH comes with a demo... We will set up a QuickNode... Congratulations, and thank you...

Continue reading
How to connect to Ethereum using PHP
Originally Published On: Jun 11, 2021
Updated On: Sep 9, 2022

PHP is a very popular choice... Before installing the web3.php,... We could use pretty much any... Now, make a PHP script file... Congratulations on connecting to...

Continue reading
How to use Subspace with QuickNode
Originally Published On: Sep 8, 2020
Updated On: Sep 9, 2022

In this guide, we'll understand... To install Subspace, we'll need... For our purposes today, we could... Now let's create a short script,... Run the file using the node...

Continue reading
How to Connect Your Dapp With MetaMask Using Ethers.js
Originally Published On: Dec 13, 2021
Updated On: Sep 9, 2022

In our dApp, we will have a... A dApp is an application that... We will need to install the... Ethers.js is a lightweight... With this application, we will... Next, we will create a function... After connecting to MetaMask, we... To handle the wallet address of... This section will show us how to... In this tutorial, we...

Continue reading
How to Lazy Mint an NFT on Rarible with Rarepress
Originally Published On: Feb 23, 2022
Updated On: Sep 9, 2022

NFTs are great for creators to... To mint an NFT, you write data... Lazy Minting is a process in... We will write some JavaScript... Rarepress interacts with the... Now that we have everything in... Congratulations on listing your...

Continue reading
How to get the balance of an ERC-20 token
Originally Published On: Jun 14, 2021
Updated On: Sep 21, 2022

When a new token is made on the... You could use any Ethereum... Now that you've done the legwork... Getting the ERC20-Token Balance... In the top of your... ABI is short for You will most likely interact... You can use a similar process... We now have a connection to an... Having made it to the end, you...

Continue reading