To guarantee uniqueness you need to come up with the ID on the server side. Then it can be for example a MySQL cell that you use to get the next number. If you have a 64bit number, you can also generate the number when the page is loaded regardless of whether in the end a unique ID is needed or not, if you need to have it always embedded in the JavaScript file. Then if the user for example saves the data, you can store the ID submitted along with it. You don't need to keep the unused ID numbers as long as you make sure the counter is reliably incrementing, so using MySQL for this you just need one cell to always increment in addition to the actual data you're saving.
With 64 bits you'll never run out of IDs and you can reorder the bits if you want it to be less predictable what the next ID will be. If any kind of security is needed, you should look more into a 1:1 mapping from the running numbers into less predictable ones.
As for where to keep the incrementing number it can be a row in any SQL database, or a simple text file if you handle locking, or if you can have a program always running on the server you can communicate with it using any kind of IPC to get new numbers. Just make sure if the server reboots the sequence will continue at least from where it was left.