JavaScript is the language so often at the core of both front-end and back-end development, making fluency in its dialects absolutely for today’s coders. Part of the reason for this proliferation across the web is for its apparent simplicity - getting started in JavaScript can feel quick and easy and starting with the “how to’s” is a great way to go. However, once you start looking deeper there are a lot of subtleties to JavaScript that are easy to overlook.
[Read More]
How To Execute JavaScript Promises In Sequence
In this tutorial, you’ll learn how to execute JavaScript promises in sequence. If you are unfamiliar with JavaScript promises, I would recommend getting a basic understanding of how JavaScript promises work.
Creating a JavaScript Promise Let’s start by creating a JavaScript promise. I’ll define a function which will return a promise which gets resolved after a specific time interval.
function createPromise(i) { let timeperiod = 1000; return new Promise((resolve, reject) => { setTimeout(() => { resolve(i) }, timeperiod) }) } As seen in the above code, I have used setTimeout to delay the response from the promise.
[Read More]
How To Insert Element To Front/Beginning Of An Array In JavaScript
In this tutorial, you’ll learn how to insert element to front/beginning of an array in JavaScript.
For adding an element to an array, you use push which inserts the new element to the end of the array.
let array_list = [1, 2, 3] array_list.push(4); console.log(array_list); Output : 1, 2, 3, 4 To insert an element to the front or beginning of an array you can use the unshift method.
let array_list = [1, 2, 3] array_list.
[Read More]
How To Read Arguments From JavaScript Functions
In this tutorial, you’ll learn How to read arguments from JavaScript functions. This question was asked to me during a JavaScript interview. Off course you can read arguments from the parameters to the function. But there is a way to read all function arguments as a single object or array list.
Using Arguments Keyword The keyword arguments gives you all the arguments passed into the JavaScript function.
function foo() { console.
[Read More]
How to Reverse a Number In JavaScript
In this tutorial, you’ll see how to reverse a number in JavaScript. There is no direct method to reverse a number in JavaScript. You’ll convert a number to string, then to array, reverse it and convert it back to number.
function reverseNumber(num){
return Number(String(num).split("").reverse().join(""))
}
console.log(reverseNumber(123))
How To Sort Array Of Objects In JavaScript
In this tutorial, you’ll learn how to sort array of objects in JavaScript. Depending on different scenarios you may be required to sort your JavaScript array objects by key or value or a date field.
Let’s start by creating an array of objects.
let arr = [ {'name' : 'Roy'}, {'name' : 'James'}, {'name' : 'Andrew'} ]; For sorting an array you can utilize the Array.prototype.sort method. Here is how you can use it:
[Read More]
How To Stub A Function Using Sinon
While doing unit testing you’ll need to mock HTTP requests and stub certain methods of the application code. In this tutorial, you’ll learn how to stub a function using sinon.
Creating A Simple JavaScript Module Let’s start by creating a folder called testLibrary. Navigate to the project directory and initialize the project.
mkdir testLib cd testLib npm init Once you have project initialized you need to create a library module which provides a method to generate random strings.
[Read More]
JavaScript : How To Get Last Character Of A String
In this quick tip, you’ll see how to get last character of a string using JavaScript. Most of the time, to work on strings you need to convert string to an array and then access string characters.
To Get Last Character Of String The following code converts string to an array:
let str = "CodeHandbook"; let arr = str.split(''); console.log(arr); Basically, the above code splits the string based on space and converts it into an array.
[Read More]
JavaScript Ternary Operator
JavaScript ternary operator is short form of writing if and else statement in JavaScript. To understand how to use ternary operator in JavaScript, let’s have a look at some conditional JavaScript code example.
let status = true; if(status){ console.log("Status is true."); } else { console.log("Status is false."); } Now let’s write the above code example using ternary operator in JavaScript.
let status = false; status ? console.log("Status is true.") : console.
[Read More]
Understanding Factories Design Pattern In Node.js
Design patterns are an important part of software development. Design pattern is a proven way of solving a certain problem. In this tutorial, you’ll try to understand factories design pattern in Node.js.
Getting Started Let’s assume that you are trying to build a web application for employee management. There are different kinds of employees available in the company. For the time being assume three kind of employees working in the company
[Read More]