Need a New Job? Find It Here!

Get personalized job alerts matching your skills and preferences.

What Happened with Faker.js - How To Use IT

Home >> Blogs >> What Happened with Faker.js - How To Use IT
Fake Data Generating JavaScript

After developing an application or software, it’s better to check its accuracy and functionality. And the best way of doing so is with the help of datasets. But it’s arduous to get a large dataset with duplicate rows and columns. Plus, it’s equally difficult to repeat the process to insert multiple rows/columns. 

So, in this scenario, what to do? Simple, you can make use of a library called Faker.js. It’s a fake data generating a library of JavaScript that can generate large datasets with ease and without human interference. And this is what all this blog is all about. 

In this blog, I’ll elaborate on everything that you should know about how to generate fake data JavaScript. And will also demonstrate an example for a finishing touch. So, let’s get right into it without further ado.

More about Faker.js

All-in-one Hiring OS

Free AI Powered ATS & Interview Solutions

Revolutionizing Interviews, Hiring, and Job Opportunities

BlogImg

So, now you have a basic understanding of what Faker JavaScript is and what it does. Explaining further, it is developed in such a manner that allows it to generate data across various fields. For example, names, addresses, images, dates, email ID, etc. 

And this data helps you test your application or software before its final deployment. Now, let’s understand it technically via some examples. Note that, in the following examples, I’ll use Faker.js faker.js commands for testing a Node application. 

Setting up Faker.js

Setting up Faker.js is straight forward. First of all, you need to install Faker.js. For this, run the following command:

$ node -v

v11.5.0

The above command returns the existing Node version, which in our case is 11.5.0.

$ npm init -y

The above command initiates a new Node application.

We always install the Faker.js module with ‘npm i faker’.

npm i faker

Faker.js examples

In the below example, I’ll generate fake user names using this library.

names.js

const faker = require(‘faker’);

Faker.js

console.log(`Employee: ${prefix} ${FirstName} ${LastName} ${suffix}`);

console.log(`Job title: ${JobTitle}`);

console.log(`Job area: ${JobArea}`);

console.log(`Phone: ${phone}`);

The above code returns the following.

Random first name, last name, job title, suffix, prefix, phone number, and job area. 

The output window will look like the following:

$ node names.js

Employee: Mr. Venice Abernathy III

Job title: Customer Data Associate

Job area: Head

Phone: 1-916-816-2332

So, this is how Faker.js works. Now, let’s fake some dates for our dataset.

Example of faking dates

dates.js

const faker = require(‘faker’);

Faker.js

console.log(futureDate);

console.log(recentDate);

console.log(weekday);

This returns random recent and future dates along with a random weekday (Monday to Friday)

Output

$ node dates.js

2019-04-13T19:09:43.672Z

2019-01-06T12:28:29.089Z

Friday

So, this is what Faker.js is capable of. Following the above two examples, you can generate fake data in various fields as required in your dataset. This library is a boon to the developers. So, try a few examples of your own to explore it further.

Related Articles

ETL Process Testing

Types of Software Development