Categories
JavaScript Answers

How to generate a short UID like “aX4j9Z” in JavaScript?

Spread the love

Sometimes, we want to generate a short UID like "aX4j9Z" in JavaScript

In this article, we’ll look at how to generate a short UID like "aX4j9Z" in JavaScript.

How to generate a short UID like "aX4j9Z" in JavaScript?

To generate a short UID like "aX4j9Z" in JavaScript, we can use the nanoid package.

To install it, we run

npm i nanoid

Then we write

import { nanoid } from "nanoid";

console.log(nanoid(11));

to call nanoid with 11 to return a random string with 11 characters.

Conclusion

To generate a short UID like "aX4j9Z" in JavaScript, we can use the nanoid package.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *