JWT - Why we don't need to store tokens in database?

I discussed a question with many people and the question was - Do you store the JWT tokens in the database?

First Answer:   Yes, we do store. Whenever we got an API call we get the token from the header and match it with the tokens that we have stored in DB.

Second Answer: No, we don't store tokens in a database we simply get them verified by JWT itself. We get the token from the header and pass it JWT verify method like jwt.verify(token, secret).

What I feel is that it is not required to store the tokens in the database. Because if you will be storing tokens in the database then you'll be setting some TTL(expiry time) there and on every request you will be verifying the token with user requested token and every time you'll update the TTL. ...  Read More

Share This:


Difference between find() and filter() in JavaScript | ES6 some() and every() functions example

Many time we need to traverse the array data while building the application. Fortunately, JavaScript provides many array functions to do that but use case of these functions are different. Here in the below description, we'll discuss the ES6 array functions find() and filter().

If we only wanted to return the first matching element in the array: We use find() for this. .find() will look and stop after the first match. Let me explain with an example:

Let say we have a products array and we wanted to find the price of TV in below example:

The output of the above script will be:

Now If we run filter() function in the same case then it will return the array of objects whose name is TV. .filter() will continue searching through the entire array.

The output of the above script will be:

find() and filter() are only used when we wanted to return the matching results and use them but if we just wanted to check the existence of matching result, in that case, we can use some() and every() ES6 functions...  Read More

Share This:


JavaScript Promise Example | JavaScript Promises | Promise.all() | Promise.race()

In JavaScript we uses promises for handling async API calls.  In JavaScript the  Promise takes one argument as a callback containing 2 parameters resolve and reject.

If promise is fulfilled the resolve function will be called and if promise is not fulfilled  reject function will be called.

Let me explain with some example:

Here in this example I have defined 2 promises one is classResult and other is gotMedal. classResult and gotMedal returns promises. If promise resolved then code block executes else catch code block executes. ...  Read More

Share This:


Copy to clipboard example in JavaScript/jQuery | How To Copy to Clipboard

copy-clipboardCopy to clipboard can be used to make easy for a user to copy a particular text. User will not need to select the text first and then copy it. This can be achieved with just a button click.

I'm going to explain copy to clipboard by using JavaScript/jQuery. It also execute copy command to copy the content.

Here is the code for copy to clipboard in JavaScript/jQuery. Like i have created a index HTML file.



index.html

When you click on copy button we get the text and pass it to copyToClipboard() function. There I created a temporary input text field and assign that message to the input text field. Then I select the input text by select() function and execute the copy command by execCommand() function. After executing the copy command I removed the temporary text field. Whenever the message is copied you will see the copied! message also. ...  Read More

Share This:


Draw Lines on Google Map using JavaScript API

draw-path-on-google-map (2)If you wanted to represent geographical locations on Google map, You might need to draw the lines on Google map. Here in this article I'll explain how to draw the path among the locations by using Google map JavaScript API.

I'll create an array of locations in PHP and I'll also define the API key in PHP. Later I'll iterate every address and draw the path among all the locations.

You can change the center of the map by changing the latitude and longitude here:

You also can change the zoom level of the map here:

Here is the source code for drawing the path on Google map:



index.php

  ...  Read More

Share This:


How to draw the route in a map with points on Google Maps

Draw path on google mapMany of the location Apps we need to draw a path from origin to destination. We need to draw path between some locations.

I'll be drawing the path by two modes. Either you are walking or you are driving. I'll be showing optimize way points for the travel path.

Here I'm using Google Map JavaScript API for drawing the route between the points on Google Map. You can draw the path among multiple points so it is not restricted to create path between two points or three points.



I have created a PHP file where I have defined the Google map API key and the way points where you can draw the route path. I have created an array of way points you also can fetch these way points from your database if needed. ...  Read More

Share This:


How to pretty JSON fromat using JavaScript/jQuery?

Pretty JSON example

Pretty JSON example

So many time we required ti print the JSON in pretty format because it is easy to read and understood. As we know JSON (JavaScript object notation)  is a lightweight data-interchange format. It is easy for humans to read and write.
In this article you will learn how to print JSON in pretty format at browser.



Here is the CSS code create a new css file named as stylesheet.css.
stylesheet.css

I have used CSS for highlighting the key, value and strings.
Here is the JavaScript and HTML code to print the JSON in pretty format.



index.html

Here in JavaScript I have cerated a function to make the JSON in pretty format. Here we have a products JSON and we are printing products JSON in pretty format. ...  Read More

Share This:


Difference between 'currying' and 'closure' in JavaScript?

So many time we got confused in currying and closure JavaScript functions just because both uses functions inside function.

So This is the answer of your question.

First I will explain What is closure in JavaScript ?

Creating a closure is nothing more than accessing a variable outside of a function's scope. Please note that a function inside a function isn't a closure. Closures are always use when need to access the variables outside the function scope.



Here is an example of closure function:

Answer will be 21. So here inside google function we are able to access the variable x which is outside the scope of the function google(). ...  Read More

Share This:


Currying functions in JavaScript | JavaScript currying function example

Currying is converting a single function of  arguments into  functions with a single argument each. So you can say Currying is a way to reduce functions of more than one argument to functions of one argument.

Here in Currying function we also uses the property of closures because we access the variables outside the scope of a function.

Here is and example of doing two number addition in JavaScript with simple function :

No if you write this with currying function in JavaScript :

Here is another which can give you more clarity on Currying function in JavaScript.

  ...  Read More

Share This:


Javascript closure function

Creating a closure is nothing more than accessing a variable outside of a function's scope. Please note that a function inside a function isn't a closure. Closures are always use when need to access the variables outside the function scope.

Closures mechanism is used to enable the data privacy.

Why is closures important?

  • Closures provide a way to associate data with a method that operates on that data.
  • They enable private variables in a global world.
  • Many patterns, including the fairly popular module pattern, rely on closures to work correctly.
  •  ...  Read More

Share This: