We can detect if the user is online or offline by using the navigator
object.
Additionally, online and offline events will be triggered if the user’s internet status changes. So, we can add an online and offline event listener to the window and perform respective actions based on the online or offline state.
Let’s create a traffic light to display the network status. By default, we will show a yellow color. Try disconnecting the network to see the changes.