How To Create A Random String With Maximum Characters in JavaScript
Tags: javascript, coding, programming, webdev
Published: 2022-01-27 07:56:25

---
title: How To Create A Random String With Maximum Characters in JavaScript
published: true
date: 2022-01-26 10:35:54 UTC
tags: javascript, coding, programming, webdev
canonical_url: https://hasnode.byrayray.dev/how-to-create-a-random-string-with-maximum-characters-in-javascript
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/balt174lq1bcqsl11lhh.jpg
---

You can use a library for generating random data in JavaScript, but I think it's even cooler to do it yourself. If you want to spice up your JavaScript skills, these functions are the best to practice.

So in this post, I would like to explain how you can generate random strings with a maximum amount of characters. This post is a follow-up to my previous post **[How To Create a Random String with JavaScript](https://hasnode.byrayray.dev/how-to-create-a-random-string-with-javascript)**.

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

If you haven't checked my previous post, I highly recommend checking it out. In this, we will continue with that last function from that post.

```javascript
function randomString() {
    return [...Array(5)].map((value) => (Math.random() * 1000000).toString(36).replace('.', '')).join('');
}

```

This function now generates a random string with a variable length.

The return value is always different.

```javascript
randomString();
'63lklfirmmc2baknxlzou171jyq7q7wnd8ag53r6kv95buvp1qme8ou'
randomString();
'j9qfsxifdugz5bgfmqfwg3c2jefsidxikhl2c4qjwti6i0zm5y5x5'
randomString();
'e6hyywpjqguf74ojsnb0ta3bynbjc3kn2dag2rf5l5caey02iudobu'
randomString();
'bhuzpkn1wx8x3p7p08mirknhj5s8alded11j5yetl7ef2o1mqfo4v'
randomString();
'8jyn22hd584ii00ny187nl5aay0vx0aa545rgfbcjdwj3znlmkqen7'

```

But right now, we want to make sure that it has a certain length so we have more control over the data we generate.

We only have to make sure that the Array it's starting with has the length.

## Minimal length

To make sure we have at least the number of characters we are given in the function parameter, we are making the Array a bit longer.

```javascript
[...Array(length + 10)]
```

## Fixed length

But to prevent that will be longer, we are taking a substring, so we are returning the exact amount of characters by using `substring`.

```javascript
[...Array(length + 10)].map((value) => (Math.random() * 1000000).toString(36).replace('.', '')).join('').substring(0, length);
```

If we turn all of this into a function, this would be our result.

```javascript
function randomString(length = 50) {
    return [...Array(length + 10)].map((value) => (Math.random() * 1000000).toString(36).replace('.', '')).join('').substring(0, length);
};
```

I would recommend playing around with this function and using it for something practical.

The next post will be another JavaScript exercise 😉.

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

## Thanks for reading!

![hashnode-footer.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629789655319/nBF6anHH4w.png)_I hope you learned something new or are inspired to create something new after reading this story!

> Did you know that you can create a [Developer blog like this one, yourself](https://hashnode.com/@devbyrayray/joinme)? It's entirely for free. 👍💰🎉🥳🔥

_If I left you with questions or something to say as a response, scroll down and type me a message. Please send me a [DM on Twitter @DevByRayRay](https://twitter.com/@devbyrayray) when you want to keep it private. My DM's are always open 😁_