How to detect online/offline status in Javascript

We can detect if the user is online or offline by using the online propertywill return true if the user is connected to the internet; otherwise, it returns false. on 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.

Attributions:
  1. undefined by undefined