There are few txt files containing a number of top English words:
~ 1000 most used English words (best for testing)
~ 5000 most used English words
~ 8000 most used English words
~ 10000 most used English words (best for playing)
~ 13000 most used English words
Those are the most used English words. I can't get a credit to the person who did this work of collecting a number of books and articles and sorting them - I downloaded those dictionaries in ~2010 and can't find the source.
You may find some of these words offensive.
You may find some remaining duplicates ('color' and 'colour')
You may find some of these words useless ('aye', 'an') or missing spaces or hyphens ('guineapig' instead of 'guinea pig').
I did my best to clean the dictionaries, but if you're going to use them commercially, make sure to check the words first.
const tmp = 'copy-paste all the words from the txt file';
const dictionary = tmp.split(', '); // ['ABLE', 'ABOUT', 'ABOVE', ...]
function getRandomWord() {
return dictionary[Math.floor(Math.random() * dictionary.length)];
}
let word = getRandomWord();
console.log(word); // HELLO
const minLength = 6;
let longerWord = '';
let counter = 0;
// run the cycle until we find a word that meets the requirements:
while (longerWord.length < minLength) {
longerWord = getRandomWord();
counter++;
if (counter > 1000) {
console.log('Failed to find a long enough word!');
break; // prevents getting into the infinite loop
} // if conditions you've set are too strict
}
console.log(longerWord); // COOKIE
You can use this code sample to also set a maximum length, or generate a word with fixed length:
const maxLength = 8;
while (longerWord.length < minLength || longerWord.length > maxLength) {...};
const fixedLength = 6;
while (longerWord.length !== fixedLength) {...};