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
🌐
p5.js
editor.p5js.org › p5 › sketches › Sound:_Load_and_Play_Sound
Sound-Load-and-Play-Sound by p5 -p5.js Web Editor
A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators, and beginners.
🌐
Howlerjs
howlerjs.com
howler.js - JavaScript audio library for the modern web
Whether a simple stereo pan or ... howler.js makes it easy to keep it all straight. ... Supports all browser-ready files: MP3, MPEG, OPUS, OGG, OGA, WAV, AAC, CAF, M4A, MP4, WEBA, WEBM, DOLBY, FLAC. ... Defaults to Web Audio and falls back to HTML5 Audio to provide full coverage across all browsers and platforms including IE9 and Cordova. ... Control everything from play, pause and seek to rate, fade and loop, just to name a few. ... Loaded sounds are automatically ...
🌐
W3Schools
w3schools.com › jsref › met_audio_play.asp
HTML DOM Audio play() Method
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › HTMLAudioElement › Audio
HTMLAudioElement: Audio() constructor - Web APIs | MDN
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. js · new Audio() new Audio(url) url Optional · An optional string containing the ...
🌐
npm
npmjs.com › package › play-sound
play-sound - npm
August 24, 2023 - var player = require('play-sound')(opts = {}) // $ mplayer foo.mp3 player.play('foo.mp3', function(err){ if (err) throw err }) // { timeout: 300 } will be passed to child process player.play('foo.mp3', { timeout: 300 }, function(err){ if (err) throw err }) // configure arguments for executable if any player.play('foo.mp3', { afplay: ['-v', 1 ] /* lower volume for afplay on OSX */ }, function(err){ if (err) throw err }) // access the node child_process in case you need to kill it on demand var audio = player.play('foo.mp3', function(err){ if (err && !err.killed) throw err }) audio.kill()
      » npm install play-sound
    
Published   Aug 24, 2023
Version   1.1.6
Author   Hrvoje Simic
🌐
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(); } }, 1000); Now, whenever the timer ends, a gentle “ding dong” sound is played.
Find elsewhere
🌐
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 - js · // get the audio element const audioElement = document.querySelector("audio"); // pass it into the audio context const track = audioContext.createMediaElementSource(audioElement); Note: The <audio> element above is represented in the DOM by an object of type HTMLMediaElement, which comes with its own set of functionality. All of this has stayed intact; we are merely allowing the sound to be available to the Web Audio API. When playing sound on the web, it's important to allow the user to control it.
🌐
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.

🌐
Flukeout
flukeout.github.io › simple-sounds
Play Sounds with JavaScript
"dead" : { url : "sounds/dead.wav" }, "ping" : { url : "sounds/ping.mp3", volume : .5 } The name of the sound above will be "dead". You can add an optional volume property, which can range from 0 to 1. To play a sound, call the method with the name of the sound.
🌐
GitHub
gist.github.com › antoniocampos › d7daaa40c3960b72dbf275525a91f83f
Javascript PlaySound · GitHub
Clone this repository at &lt;script src=&quot;https://gist.github.com/antoniocampos/d7daaa40c3960b72dbf275525a91f83f.js&quot;&gt;&lt;/script&gt; Save antoniocampos/d7daaa40c3960b72dbf275525a91f83f to your computer and use it in GitHub Desktop. Download ZIP · Javascript PlaySound ·
🌐
W3Schools
w3schools.com › html › html5_audio.asp
W3Schools.com
This allows you to load, play, and pause audios, as well as set duration and volume.
🌐
Medium
mcasimirian.medium.com › playing-audio-on-click-in-your-javascript-app-72aa955068fc
Playing Audio on Click in Your JavaScript App | by Manon Sainton | Medium
May 12, 2021 - In the index.html file, there is a script tag set to JavaScript so that my .js file could be read, interpreted, and displayed to the DOM. Next, we need to find some sound clips! FreeSound.org is a great site that has a variety of cool clips. You may have to create a free account before having access to sounds. Choose something that doesn’t have a delay at the start and is decent quality. For this small project, I chose two sounds: one clip that says “Play” and another that says “Stop”. Now let’s get some code into the editor!
🌐
Programming Head
programminghead.com › how-to-play-audio-in-html-using-javascript
How to Play Audio in HTML using JavaScript | Audio Player
To play sound when you click the HTML image, then you can simply use JavaScript onClick event in your Image and then by using that Image, you can play the Audio file.
🌐
GitHub
github.com › Marak › play.js
GitHub - Marak/play.js: play sound files from node.js to your speakers, simple as cake and kid approved! · GitHub
var play = require('play').Play(); // play with a callback play.sound('./wavs/sfx/intro.wav', function(){ // these are all "fire and forget", no callback play.sound('./wavs/sfx/alarm.wav'); play.sound('./wavs/sfx/crinkle.wav'); play.sound('./wavs/sfx/flush.wav'); play.sound('./wavs/sfx/ding.wav'); }); //If you want to know when the player has defintely started playing play.on('play', function (valid) { console.log('I just started playing!'); }); play.sound('./wavs/sfx/ding.wav'); //If you want to know if this can't play for some reason play.on('error', function () { console.log('I can't play!'); }); ... Recommended that you use play.min.js for web-based playbacks assumes you have already embedded the audio file as an embed tag
Starred by 161 users
Forked by 39 users
Languages   JavaScript
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Games › Techniques › Audio_for_Web_Games
Audio for Web games - Game development - MDN Web Docs
January 25, 2026 - To play a specific sound from the file, we just use the known start and stop times for each audio sprite. The advantage is that we can prime one piece of audio and have our sprites ready to go. To do this we can just play and instantly pause the larger piece of audio.
🌐
GitHub
gist.github.com › 3953391
Play a sound with JavaScript (html5) · GitHub
Clone this repository at &lt;script src=&quot;https://gist.github.com/toast38coza/3953391.js&quot;&gt;&lt;/script&gt; Save toast38coza/3953391 to your computer and use it in GitHub Desktop. Download ZIP · Play a sound with JavaScript (html5) Raw · play_sound.html ·
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-play-a-mp3-file-in-js
JavaScript- Play a .mp3 File in JS - GeeksforGeeks
July 23, 2025 - The <audio> tag is used to embed sound files, and its src attribute points to the .mp3 file you want to play. The JS the audio player and play button using getElementById.