IMPORTANCE Atrial fibrillation (AF) has been suggested as a risk factor for dementia since it may lead to chronic cerebral hypoperfusion and stroke. However, longitudinal studies assessing the association between AF and dementia have shown inconsistent results. OBJECTIVE To determine the effect of AF on the risk of developing dementia during 20 years of follow-up. DESIGN, SETTING, AND PARTICIPANTS The association of prevalent and incident AF with incident dementia was assessed from July 6, 1989, to February 4, 2010, in 6514 dementia-free participants in the prospective population-based Rotterdam Study. Data analysis was conducted from September 18, 2014, to April 17, 2015. Cox proportional hazards regression models adjusting for age, sex, and cardiovascular risk factors; censored for stroke; and stratified by median age were used. In addition, we investigated whether the association between incident AF and dementia varied according to the duration of exposure, categorized in 6-year time bands. EXPOSURES Prevalent and incident AF. MAIN OUTCOMES AND MEASURES Incident dementia, determined according to the Diagnostic and Statistical Manual of Mental Disorders (Third Edition Revised) and the National Institute of Neurological and Communicative Disorders and Stroke-Alzheimer's Disease and Related Disorders Association criteria. RESULTS At baseline, 318 of 6514 participants (4.9%) had prevalent AF, and during 81 483 person-years of follow-up, 994 participants (15.3%) developed incident dementia. With findings presented as adjusted hazard ratio (95% CI), prevalent AF was related to an increased risk of dementia (1.33; 1.02-1.73). Among 6196 participants without prevalent AF during 79 003 person-years of follow-up, 723 participants (11.7%) developed incident AF and 932 individuals (15.0%) developed incident dementia. Incident AF was associated with an increased risk of dementia in younger participants (<67 years: 1.81; 1.11-2.94 vs >= 67 years: 1.12; 0.85-1.46; P = .02 for interaction). The risk of dementia was strongly associated with duration of exposure to AF in the younger participants (in the highest stratum: 3.30; 1.16-9.38; P = .003 for trend) but not in the elder participants (0.25; 0.04-1.86; P = .94 for trend). CONCLUSIONS AND RELEVANCE Atrial fibrillation is associated with an increased risk of dementia, independent of clinical stroke. This association was strongest for younger participants with the longest duration of AF. Future studies should investigate whether optimal treatment of AF can prevent or postpone dementia.