ImportanceThe onset age of nonalcoholic fatty liver disease (NAFLD) is decreasing, and whether earlier ages of NAFLD onset are associated with increased cancer risk is currently unclear.ObjectiveTo explore the association between NAFLD new-onset age and cancer risk.Design, Setting, and ParticipantsThis cohort study was conducted among 179 328 participants included in the Kailuan Cohort Study between 2006 and 2021. In total, 46 100 incident NAFLD cases were identified. For each case, a participant matched by age (older or younger by 1 year) and sex was randomly selected to create a new matched study cohort. Data were analyzed from December 2022 through April 2023.ExposureOnset of NAFLD.Main Outcomes and MeasuresThe association between the onset age of NAFLD and the risk of different cancer types was evaluated using weighted Cox regression models. Population-attributable fractions (PAFs) were used to quantify the association of NAFLD with cancer risk at different ages.ResultsAmong 63 696 participants (mean [SD] age, 51.37 [12.43] years; 10 932 females [17.2%] and 52 764 males [82.8%]), 31 848 individuals had NAFLD and 31 848 individuals were in the control group. During a median (IQR) follow-up of 10.16 (7.89-11.67) years, 2415 patients were diagnosed with cancer. Compared with the matched group, patients aged less than 45 years at NAFLD onset exhibited a higher risk of cancer (average hazard ratio [AHR], 1.52; 95% CI, 1.09-2.12), and as the onset age of NAFLD increased, the cancer risk decreased (ages 45-54 years: AHR, 1.50; 95% CI, 1.15-1.97; ages 55-64 years: AHR, 1.13; 95% CI, 0.97-1.33; ages >65 years: AHR, 0.75; 95% CI, 0.45-1.27; P for interaction < .001). Among patients aged less than 45 years at NAFLD onset, cancers were mainly digestive system and lung cancers, with AHR values of 2.00 (95% CI, 1.08-3.47) and 2.14 (95% CI, 1.05-4.36), respectively. PAFs also showed that in patients aged less than 45 years at NAFLD onset, 17.83% (95% CI, 4.92%-29.86%) of cancer risk was attributable to NAFLD.Conclusions and RelevanceThis study found that NAFLD was associated with increased cancer risk and there was an interaction with onset age, such that the younger the onset age of NAFLD, the greater the cancer risk.