Categories
JavaScript Answers

How to play a notification sound on websites with JavaScript?

Spread the love

Sometimes, we want to play a notification sound on websites with JavaScript.

In this article, we’ll look at how to play a notification sound on websites with JavaScript.

How to play a notification sound on websites with JavaScript?

To play a notification sound on websites with JavaScript, we can create a new audio element and call play on it.

For instance, we write

const playSound = (url) => {
  const audio = new Audio(url);
  audio.play();
};

to create the playSound function that creates a new Audio object with the source being the audio file at the url.

Then we call play on it to play the audio at the url.

Conclusion

To play a notification sound on websites with JavaScript, we can create a new audio element and call play on it.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *