Rengga Dev – Math.random()
is an API in JavaScript. It is a function that gives you a random number. The number returned will be between 0 (inclusive, as in, it’s possible for an actual 0 to be returned) and 1 (exclusive, as in, it’s not possible for an actual 1 to be returned).
We replace the header with a randomly selected word from an array using Math.random
:
var word = words[Math.floor(Math.random() * words.length)] + "!";
This is a lot like the random image example — the perfect sort of practice for beginners!