Create a Unique ID by JavaScript Function and Store into Variable

 

Create Unique ID by JavaScript

 

There are different way to generate Unique ID or unique identifier number in JS. Its very useful and use in may cases rendering list or identify recordsd in database.

  <script type="text/javascript">

    function createUniqueId(){
        const head = Date.now().toString(36);
        const tail = Math.random().toString(36).substr(2);
        const uniqueId = head+tail;
        return uniqueId;     
    }

    const getUniqueId = createUniqueId(); 
    console.log(getUniqueId);    
        
    const uniqueId = document.getElementById('uniqueId');    
    uniqueId.innerHTML = getUniqueId;    
        
    </script>