How to clear setTimeout() in JavaScript?

In order to clear setTimeout():

  1. Assign the setTimeout() to the variable.
  2. Use the clearTimeout() function on the variable.

Example:

<script>
    var myTimeout = setTimeout(function() {
        console.log('test');
    }, 500)

    clearTimeout(myTimeout);
</script>