Javascript code:
Output: