HTML Audio & Video
HTML DOM Audio ended Property
The ended property returns whether the media element has finished playing, that is, whether playback has reached the end of the media. It is a read-only boolean.
Definition and Usage
ended is true once playback reaches the end during normal forward playback and is false at all other times. It is closely related to the ended event, which fires at the same moment and is usually the more convenient way to react.
It returns true after the media has played to completion, and false while playing, paused mid-way, or before playback starts. If loop is enabled, playback restarts instead and ended never becomes true.
Syntax
mediaElement.endedExample
This runnable page reports when the clip finishes and reads the ended property inside the handler. Let the short video play to the end to see the message.
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>ended Demo</title></head>
<body>
<h2>Detecting the end of playback</h2>
<video id="myVideo" width="320" controls>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>
<p><button onclick="check()">Has it ended?</button></p>
<p id="output">Play the clip to the end.</p>
<script>
const video = document.getElementById("myVideo");
const out = document.getElementById("output");
function check() {
out.textContent = "ended = " + video.ended;
}
video.addEventListener("ended", () => {
out.textContent = "Finished! ended = " + video.ended; // true
});
</script>
</body>
</html>More Examples
The ended event is the natural place to advance a playlist to the next clip.
<!DOCTYPE html>
<html>
<body>
<video id="v" width="320" controls autoplay muted>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>
<p id="msg"></p>
<script>
const v = document.getElementById("v");
const playlist = [
"https://www.w3schools.com/html/mov_bbb.mp4",
"https://www.w3schools.com/html/movie.mp4"
];
let index = 0;
v.addEventListener("ended", () => {
index = (index + 1) % playlist.length;
v.src = playlist[index];
v.play();
document.getElementById("msg").textContent = "Now playing clip " + index;
});
</script>
</body>
</html>If the loop property is enabled, the media seeks back to the start automatically and the ended event does not fire, so ended stays false.
Key Takeaways
- ended is a read-only boolean that is true only after playback reaches the end.
- It is false while playing, paused mid-way, or before playback starts.
- The ended event fires at the same moment and is usually easier to use.
- Use the ended event to advance playlists or restart custom loops.
- With loop enabled, playback restarts and ended never becomes true.
