How to generate a unique random ID using JavaScript?

In JavaScript we can generate a unique ID in many different ways. If we can consider ES6 then using crypto API we can generate a unique ID. Following is the function definition of generating a unique ID in JavaScript.