The polarizations of prompt and non-prompt J/ψ and ψ(2S) mesons are measured in proton-proton collisions at √s = 13 TeV, using data samples collected by the CMS experiment in 2017 and 2018, corresponding to a total integrated luminosity of 103.3 fb−1. Based on the analysis of the dimuon decay angular distributions in the helicity frame, the polar anisotropy, λθ, is measured as a function of the transverse momentum, pT, of the charmonium states, in the 25–120 and 20–100 GeV ranges for the J/ψ and ψ(2S), respectively. The non-prompt polarizations agree with predictions based on the hypothesis that, for pT ≳ 25 GeV, the non-prompt J/ψ and ψ(2S) are predominantly produced in two-body B meson decays. The prompt results clearly exclude strong transverse polarizations, even for pT exceeding 30 times the J/ψ mass, where λθ tends to an asymptotic value around 0.3. Taken together with previous measurements, by CMS and LHCb at √s = 7 TeV, the prompt polarizations show a significant variation with pT, at low pT