If you don't want to mess with HTML elements:

var audio = new Audio('audio_file.mp3');
audio.play();

function play() {
  var audio = new Audio('https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3');
  audio.play();
}
<button onclick="play()">Play Audio</button>

This uses the HTMLAudioElement interface, which plays audio the same way as the <audio> element.


If you need more functionality, I used the howler.js library and found it simple and useful.

<script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.1.1/howler.min.js"></script>
<script>
    var sound = new Howl({
      src: ['https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3'],
      volume: 0.5,
      onend: function () {
        alert('Finished!');
      }
    });
    sound.play()
</script>

Answer from Uri on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › HTMLAudioElement › Audio
HTMLAudioElement: Audio() constructor - Web APIs | MDN
March 11, 2024 - The Audio() constructor creates and returns a new HTMLAudioElement which can be either attached to a document for the user to interact with and/or listen to, or can be used offscreen to manage and play audio.
Discussions

Trying to get audio to play using javascript.
Never mind I solved it. I was trying to load the audio relative to the js file rather than relative to the html file. Idiot move. Still not working because of autoplay permissions, but at least I know what's going on now. More on reddit.com
🌐 r/learnjavascript
6
14
April 6, 2022
Audio Play Error with Javascript
Hi - For some reason this seemingly very straightforward javascript is not working: var myAudio = document.getElementById("audio1"); myAudio.play(); I made a test project that has it begin called from within another javascript function because that is how I need it to be called. More on forums.tumult.com
🌐 forums.tumult.com
1
0
July 5, 2018
html5 - Play audio in javascript with a good performance - Game Development Stack Exchange
Currently i'm using this code to play sounds in JavaScript: More on gamedev.stackexchange.com
🌐 gamedev.stackexchange.com
August 2, 2013
Play sound on click, then go to page
Hello! I am making an quiz in HTML with questions and 4 answers by each question. By clicking on correct answer, I want to play a short sound, then go to next html page. How can I do it to play sound on click? Please help! More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
May 20, 2021
🌐
UCI Music
music.arts.uci.edu › dobrian › webaudio › tutorials
Tutorials: HTML, JavaScript, and Web Audio
You can create and control an HTML element within JavaScript as an object. This shows how to create an audio element within JavaScript, and how to control its playback rate from the Web page. It's also possible to play sound files using the Web Audio API.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Games › Techniques › Audio_for_Web_Games
Audio for Web games - Game development | MDN
January 25, 2026 - For other more active sounds that are to be used during the game we could consider priming them as soon as something like a Start button is pressed. To prime audio like this we want to play a part of it; for this reason it is useful to include a moment of silence at the end of your audio sample. Jumping to, playing, and then pausing that silence will mean we can now use JavaScript to play that file at arbitrary points.
🌐
Reddit
reddit.com › r/learnjavascript › trying to get audio to play using javascript.
r/learnjavascript on Reddit: Trying to get audio to play using javascript.
April 6, 2022 -

I'm trying to get an audio effect to play with a mouseover event but not having any luck.

Following https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement the following code should "just work" by the looks of it but I'm getting the following error message, which I'm not really sure exactly what it's telling me.

"Uncaught (in promise) DOMException: The media resource indicated by the src attribute or assigned media provider object was not suitable."

Code is:

const menuHover = new Audio("../audio/menu1.mp3");

function playSound() {
    menuHover.play();
}

function hoverListener() {
    const menuItem = document.querySelectorAll('.menu-item');
    menuItem.forEach(item => {
        item.addEventListener("mouseover", playSound)
    })
}

Any hints on where I should be looking appreciated.

🌐
W3Schools
w3schools.com › tags › tag_audio.asp
HTML audio tag
The <audio> tag is used to embed sound content in a document, such as music or other audio streams.
Find elsewhere
🌐
Go Make Things
gomakethings.com › how-to-play-a-sound-with-javascript
How to play a sound with JavaScript | Go Make Things
July 19, 2021 - // Run a callback function once every second let timer = setInterval(function () { // Reduce count by 1 count--; // Update the UI if (count > 0) { app.textContent = count; } else { app.textContent = '⏰'; clearInterval(timer); playSound(); ...
🌐
Squash
squash.io › how-to-play-audio-files-using-javascript
How to Play Audio Files Using JavaScript
December 3, 2023 - A detailed guide on how to use JavaScript to play audio files in your web applications.
🌐
W3Schools
w3schools.com › html › html5_audio.asp
W3Schools.com
The HTML <audio> element is used to play an audio file on a web page.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Play sound on click, then go to page - JavaScript - The freeCodeCamp Forum
May 20, 2021 - Hello! I am making an quiz in HTML with questions and 4 answers by each question. By clicking on correct answer, I want to play a short sound, then go to next html page. How can I do it to play sound on click? Please he…
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › Media › Guides › Autoplay
Autoplay guide for media and Web Audio APIs - Media | MDN
The term autoplay refers to any feature that causes media to begin to play without the user specifically requesting that playback begin. This includes both the use of HTML attributes to autoplay media as well as the use of JavaScript code to start playback outside the context of handling user input.
🌐
Videojs
videojs.org
Video.js | Open Source Video Player
The open-source video player for React and HTML. Lightweight, accessible components built for performance and streaming.
🌐
UCI Music
music.arts.uci.edu › dobrian › webaudio › tutorials › JavaScript › createandplayhtmlaudioelement.html
Create and Play an HTMLAudioElement in JavaScript
March 15, 2025 - An HTMLAudioElement is a type of HTMLMediaElement. It allows you to access, open, load, and play an audio file in JavaScript, just as you can do with the <audio> tag in HTML, which provides you scripting access to the properties, methods, and events of the Audio element.
🌐
SitePoint
sitepoint.com › javascript
How I play sound clip in mozilla - JavaScript - SitePoint Forums | Web Development & Design Community
February 19, 2012 - <script> //function PlaySound() { var sound = document.getElementById(sound1); sound.Play(); alert("ok");} function playSound( url ){ var s=document.getElementById("sound").innerHTML="<embed src='"+url+"' hidden=…
🌐
Replit
blog.replit.com › replit blog › we built a video rendering engine by lying to the browser about what time it is
Replit — We Built a Video Rendering Engine by Lying to the Browser About What Time It Is
3 weeks ago - How Replit turns any web page into a deterministic video by virtualizing time itself, patching key browser audio APIs, and waging war against headless Chrome's quirks.
🌐
SitePoint
sitepoint.com › javascript
Play sound on hyperlink? - JavaScript - SitePoint Forums | Web Development & Design Community
April 12, 2014 - Hi guys I’m using this script to play a sound when a link is pressed (I’m viewing it in an iPad): <!--[ In the head section of the HTML ]--> <!--audio--> <script type="text/javascript"> function play_single_sound() {…
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Web_Audio_API › Using_Web_Audio_API
Using the Web Audio API - Web APIs | MDN
September 18, 2025 - Depending on the use case, there's a myriad of options, but we'll provide functionality to play/pause the sound, alter the track's volume, and pan it from left to right. Controlling sound programmatically from JavaScript code is covered by browsers' autoplay support policies, as such is likely ...
🌐
Newhavenarts
newhavenarts.org › arts-paper › articles › reaching-back-to-their-roots-a-quartet-finds-light-in-the-darkness
A Quartet Finds Light In The Darkness
February 16, 2026 - As they made their way to the front of the room, waves of applause suggested that it was going to be a good night. As musicians sat, the room felt like a homecoming, the sound of tuning so natural to the space that the concert would have seemed incomplete without it.