ImportanceSystemic inflammation has been suggested to explain reported associations between infections and dementia. Associations between autoimmune diseases and dementia also suggest a role for peripheral systemic inflammation.ObjectiveTo investigate the associations of infections and autoimmune diseases with subsequent dementia incidence and to explore potential shared signals presented by the immune system in the 2 conditions.Design, Setting, and ParticipantsThis nationwide, population-based, registry-based cohort study was conducted between 1978 and 2018 (40-year study period). All Danish residents born 1928 to 1953, alive and in Denmark on January 1, 1978, and at age 65 years were included. Persons with prior registered dementia and those with HIV infections were excluded. Data were analyzed between May 2022 and January 2023.ExposuresHospital-diagnosed infections and autoimmune diseases.Main Outcomes and MeasuresAll-cause dementia, defined as the date of a first registered dementia diagnosis after age 65 years in the registries. Poisson regression with person-years at risk as an offset variable was used to analyze time to first dementia diagnosis.ResultsA total of 1 493 896 individuals (763 987 women [51%]) were followed for 14 093 303 person-years (677 147 [45%] with infections, 127 721 [9%] with autoimmune diseases, and 75 543 [5%] with dementia). Among individuals with infections, 343 504 (51%) were men, whereas among those with autoimmune diseases, 77 466 (61%) were women. The dementia incidence rate ratio (IRR) following any infection was 1.49 (95% CI, 1.47-1.52) and increased along with increasing numbers of infections in a dose-dependent manner. Dementia rates were increased for all infection sites in the short term, but not always in the long term. The dementia IRR following any autoimmune disease was 1.04 (95% CI, 1.01-1.06), but no dose-dependent increase was observed, and only a few autoimmune conditions showed increased IRRs for dementia.Conclusions and RelevanceThese findings may point toward a role for infection-specific processes in the development of dementia, rather than general systemic inflammation, as previously hypothesized. Assessing these 2 conditions in a single setting may allow for additional insights into their roles in dementia and for hypotheses on possible underlying mechanisms.