How to Generation Random Fake Name with Faker.js in Javascript library

How to Generation Random Fake Name with Faker.js in Javascript library

If you’re looking for javascript library to generate fake name or dummy personal info such as email, street address, company and etc. Faker.js is the one you can’t missed. In this post, we learn more How to Generation Random Data with Node.js and Faker.js

If you’re looking for javascript library to generate fake name or dummy personal info such as email, street address, company and etc. Faker.js is the one you can’t missed.

Faker.js allows you to generate a various type of random data. Such as names , email address, profile avatar, street address, bank account, company, job title and so much more. This is very useful for driving the test or populating the data to your mock website. Let’s go check it out!

Setup

Faker.js is available for both server-side and browser-side. So if you want to implement it for a web browser just downlaod the latest release from github and plug it in to your web page.

var faker = require('faker');
//or
<script src="faker.min.js"></script>
Usage

Using faker.js is very easy. Just call any API method that you want to use. For example if you want to random a full name  or email address, just call

var randomName = faker.name.findName(); // Harry Potter
var randomEmail = faker.internet.email(); // [[email protected]](/cdn-cgi/l/email-protection)

Now let’s add some more code to show the power of faker.js. I’m going to random a person name and the company and avatar. I’ll use jQuery to create a new div element for each person and append it to page.

<div id="wrapper">
</div>
<script>
for (var i=0 ;i<10;i++) {
  jQuery('<div/>', {
  text: faker.name.findName() + '-' + faker.company.companyName()
  }).appendTo('#wrapper');

  jQuery('<img/>', {
  src: faker.image.avatar()
  }).appendTo('#wrapper');
}
</script>

And here is the result

Another cool feature of faker.js is that you can switch between the locale easily. For example if I want to random a korean person name, just use

faker.locale = 'ko';

And the result…

Faker.js is also very useful to generate a large quantity of data which you can use it to populate the database for testing purpose. Here is the example of Faker.js combined with Node.js

var faker = require('faker');
var fs = require('fs');
var str = "";

for (var i=0;i<100;i++)
  str += faker.name.firstName() + '\t' + faker.name.lastName() + '\t' + faker.internet.email() + '\t' + faker.name.jobTitle() + '\t' + faker.random.locale() +"\r\n";

fs.writeFile('c:/test.txt',str,function(err) { 
  if(err) return console.log(err); else console.log('file saved')
});

And within 5 minutes, I now have a text file containing the list of random person names ,email addresses, job title and nationality. Which can be opened in excel – ready to be imported to any databases.

Thank you for reading ! If you like this tutorial please share it with others.

JavaScript Tutorial: if-else Statement in JavaScript

JavaScript Tutorial: if-else Statement in JavaScript

This JavaScript tutorial is a step by step guide on JavaScript If Else Statements. Learn how to use If Else in javascript and also JavaScript If Else Statements. if-else Statement in JavaScript. JavaScript's conditional statements: if; if-else; nested-if; if-else-if. These statements allow you to control the flow of your program's execution based upon conditions known only during run time.

Decision Making in programming is similar to decision making in real life. In programming also we face some situations where we want a certain block of code to be executed when some condition is fulfilled.
A programming language uses control statements to control the flow of execution of the program based on certain conditions. These are used to cause the flow of execution to advance and branch based on changes to the state of a program.

JavaScript’s conditional statements:

  • if
  • if-else
  • nested-if
  • if-else-if

These statements allow you to control the flow of your program’s execution based upon conditions known only during run time.

  • if: if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
    Syntax:
if(condition) 
{
   // Statements to execute if
   // condition is true
}

Here, condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements under it.
If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then by default if statement will consider the immediate one statement to be inside its block. For example,

if(condition)
   statement1;
   statement2;

// Here if the condition is true, if block 
// will consider only statement1 to be inside 
// its block.

Flow chart:

Example:

<script type = "text/javaScript"> 

// JavaScript program to illustrate If statement 

var i = 10; 

if (i > 15) 
document.write("10 is less than 15"); 

// This statement will be executed 
// as if considers one statement by default 
document.write("I am Not in if"); 

< /script> 

Output:

I am Not in if
  • if-else: The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
    Syntax:
if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}


Example:

<script type = "text/javaScript"> 

// JavaScript program to illustrate If-else statement 

var i = 10; 

if (i < 15) 
document.write("10 is less than 15"); 
else
document.write("I am Not in if"); 

< /script> 

Output:

i is smaller than 15
  • nested-if A nested if is an if statement that is the target of another if or else. Nested if statements means an if statement inside an if statement. Yes, JavaScript allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
    Syntax:
if (condition1) 
{
   // Executes when condition1 is true
   if (condition2) 
   {
      // Executes when condition2 is true
   }
}

Example:

<script type = "text/javaScript"> 

// JavaScript program to illustrate nested-if statement 

var i = 10; 

if (i == 10) { 

// First if statement 
if (i < 15) 
	document.write("i is smaller than 15"); 

// Nested - if statement 
// Will only be executed if statement above 
// it is true 
if (i < 12) 
	document.write("i is smaller than 12 too"); 
else
	document.write("i is greater than 15"); 
} 
< /script> 

Output:

i is smaller than 15
i is smaller than 12 too
  • if-else-if ladder Here, a user can decide among multiple options.The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
if (condition)
    statement;
else if (condition)
    statement;
.
.
else
    statement;


Example:

<script type = "text/javaScript"> 
// JavaScript program to illustrate nested-if statement 

var i = 20; 

if (i == 10) 
document.wrte("i is 10"); 
else if (i == 15) 
document.wrte("i is 15"); 
else if (i == 20) 
document.wrte("i is 20"); 
else
document.wrte("i is not present"); 
< /script> 

Output:

i is 20

Learn JavaScript Crash Course | Best Javascript Tutorial

Learn JavaScript Crash Course | Best Javascript Tutorial

Take the first steps to mastering JavaScript, one of the fastest growing languages globally today. Because of its flexibility, it is there everywhere today: be it the web, mobile and or the desktop. Want to be one of the over 50,000 people who have benefited from this course? Join the learning fun!

Description
So you need to learn Javascript and learn it fast!

Are you a designer who's now adding JavaScript to your knowledge arsenal? Are you a student who needs a fast and high-quality JavaScript course? Are you a traditional developer trying to learn the basics? Are you applying for a job that requires JavaScript.

JavaScript is not only emerging as the most important language to know, it's flexibility is unique. With JavaScript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know. As a JavaScript developer you'll find opportunity around every corner!

This is a crash course meaning it moves quick. Every meaningful lecture is chock full of information that you can immediately apply to your coding work. Taught by a master instructor, you'll find that Javascript: Crash Course is a extremely efficient way to learn.

Each lecture course lecture includes the actual code developed by the master instructor during the lecture. No more staring at a blank screen when figuring out how to practice use the included code as a starting point.

The course culminates with developing a full quiz application called Trivia. You'll learn from building out this application yourself and modifying it and making it your own!

Join the over 500,000 learners who've enjoyed LearnToProgram courses. At LearnToProgram, we're teaching the world to code!

Who is the target audience?

Newbies Who Want to Learn Javascript Quickly
Current Programmers who need a Crash Course in Javascript
Those with Limited Time who Want to Learn Alot
Teacher and Students
Designers and Developers who Want to Learn JavaScript Efficiently
Basic knowledge
Foundational HTML
Text editor
Web Browser
What will you learn
Understand and write Javascript Code
Run Javascript Code in a Browser
Choose and Utilize Tools Used by Professional Javascript Developers
Understand the Purpose of Javascript Variables
Declare and Initialize Javascript Variables
User Variables with Strings and Numbers
Perform Mathematical Operations with Javascript
Obtain input from the User via Alert Boxes
Obtain input from the User via Forms
Understand Simple Conditionals with If
Create Complex Conditionals with If Else If
Create While Loops
Create For Loops
Understand the Purpose and Power of Javascript Objects
Use Event Objects to Track Events that Occur
Use the Javscript Date Object to Track Dates
Use the Javascript String Object to Manipulate Strings
Include Audio and Video Content with the Media API
Use the XMLHttpRequest() Object to Exchange Data
Create a Basic Ajax Request
Parse JSON code

How to get started Internationalization in JavaScript with NodeJS

How to get started Internationalization in JavaScript with NodeJS

Tutorial showing how to use the Intl JS API in NodeJS (i18n). We'll install a module to unlock the Intl API languages for Node and test out RelativeTimeFormat to translate and localise relative times in JavaScript.

Tutorial showing how to use the Intl JS API in NodeJS (i18n). We'll install a module to unlock the Intl API languages for Node and test out RelativeTimeFormat to translate and localise relative times in JavaScript. I'll tell you how to get started with the built-in internationalization library in JS for Node 12 and higher. We'll change the locale to see how the translation works and test different BCP 47 language tags.

Internationalization is a difficult undertaking but using the Intl API is an easy way to get started, it's great to see this new API in the JS language and available for use. Soon, you'll be able to have confidence using it in the browser as modern browsers support the major Intl features. Have a look at the browser compatibility charts to see which browsers and versions of node are supported.

Use Intl.RelativeTimeFormat for language-sensitive relative time formatting.
#javascript #nodejs #webdevelopment

MDN Documentation:

https://developer.mozilla.org/en-US/d...

Full ICU NPM package:

https://www.npmjs.com/package/full-icu