Cosmological and astrophysical probes of axionlike particles