Checking if internet connection is up

Regardless of the state of wifi and mobile network, it would be nice to have a function to know if internet is actually connected. I found cases where yes, wifi or mobile data was connected but browsing or messaging was not possible due to lack to internet access.

A function like nc(isinternet) that returns true or false would be helpful.