The navigator.connection object contains a read-only property named effectiveType, which returns the effective connection type. That type is determined using a combination of recently observed RTT (round-trip time) and downlink values. (at the time of writing, browser support is limited)
Full article