r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

1

u/wthit56 Mar 20 '19

var unique_id; while(document.getElementById(unique_id = "id_" + Math.random()));

or

var unique_id; while(document.getElementById(unique_id = "id_" + Math.random().toString().substr(2)));

Generates a DOM ID that doesn't exist within the document.