I would like to generate a unique id with javascript without exceeding an integer type.
I see it: create unique id with javascript
With the following solution: (new Date()).getTime() but out of the range of an integer.
Thanks for your suggestion.
I would like to generate a unique id with javascript without exceeding an integer type.
I see it: create unique id with javascript
With the following solution: (new Date()).getTime() but out of the range of an integer.
Thanks for your suggestion.
Depends on what size integer you want. Here is a fiddle that shows a couple of ways to do it: http://jsfiddle.net/leemeador/C6YGa/
The idea is to do something like this for a 32 bit integer:
var i = new Date().getTime();
i = i & 0xffffffff;
0xffff what is the range? Thanks.Another way to generate unique key (id) with new ES6 Syntax, by returning a Number representing the milliseconds elapsed since the UNIX epoch:
export const generateRandomKey = () => Date.now();
You can write it in separate file and export. than import to your file using es6 syntax.
import {generateRandomKey} from './the/file/path.js';
(pretty much any expression) | 0will return a value from 0 to 2^32-1.(expr) >>> 1will half that range.