A search for heavy neutral leptons (HNLs), the right-handed Dirac or Majorana neutrinos, is performed in final states with three charged leptons (electrons or muons) using proton-proton collision data collected by the CMS experiment at $$ \sqrt{s} $$= 13 TeV at the CERN LHC. The data correspond to an integrated luminosity of 138 fb−1. The HNLs could be produced through mixing with standard model neutrinos ν. For small values of the HNL mass (