The measurements of the inclusive J/ψ yield at midrapidity (|y|<0.9) and forward rapidity (2.5 <y< 4) in Pb–Pb collisions at sNN=5.02 TeV with the ALICE detector at the LHC are reported. The inclusive J/ψ production yields and nuclear modification factors, RAA, are measured as a function of the collision centrality, J/ψ transverse momentum (pT), and rapidity. The J/ψ average transverse momentum and squared transverse momentum (〈pT〉 and 〈pT2〉) are evaluated as a function of the centrality at midrapidity. Compared to the previous ALICE publications, here the entire Pb–Pb collisions dataset collected during the LHC Run 2 is used, which improves the precision of the measurements and extends the pT coverage. The pT-integrated RAA shows a hint of an increasing trend towards unity from semicentral to central collisions at midrapidity, while it is flat at forward rapidity. The pT-differential RAA shows a strong suppression at high pT with less suppression at low pT where it reaches a larger value at midrapidity compared to forward rapidity. The ratio of the pT-integrated yields of J/ψ to those of D0 mesons is reported for the first time for the central and semicentral event classes at midrapidity. Model calculations implementing charmonium production via the coalescence of charm quarks and antiquarks during the fireball evolution (transport models) or in a statistical approach with thermal weights are in good agreement with the data at low pT. At higher pT, the data are well described by transport models and a model based on energy loss in the strongly-interacting medium produced in nuclear collisions at the LHC.