There is no native function for MD5 encryption in JavaScript. Fortunately, there are clever programmers who have prepared and made available their own functional solution that we can use. Here is example with the blueimp library.
blueimp / JavaScript-MD5
JavaScript MD5 implementation by Sebastian Tschan. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.
Download library md5.js or md5.min.js from Github: blueimp.github.io/javascript-md5
<script src="md5.min.js"></script>
<script>
let str = 'Hello world!';
var strHash = md5(str);
console.log('The MD5 hash is: ' + strHash);
</script>
The above example will output:
The MD5 hash is: 86fb269d190d2c85f6e0468ceca42a20
Example #2 with salt before string to be encrypted:
In some cases, it is worth using a salted MD5 hash for added security. This means that you add any "salt" before or after the string to be encrypted.
<script src="md5.min.js"></script>
<script>
let salt = 'i#8^*uu'; //your arbitrary secret key
let str = 'Hello world!';
var strHash = md5(salt + str);
console.log('Salted MD5 hash is: ' + strHash);
</script>
Example #2 will output:
Salted MD5 hash is: 5e5eb686808a7c39710dccf1ce03a4e6