This study examines the association between age at marital loss (i.e., divorce or widowhood) and cognitive function in later life and whether the association differs by gender. We used mixed-effects models, drawing on longitudinal data from the Health and Retirement Study (1998-2016). The analytical samples included adults aged 51 and older who had ever been widowed (N = 5,639 with 25,537 person-waves) or divorced (N = 10,685 with 50,689 person-waves). We find that those who were widowed at younger ages had lower cognitive function than their counterparts who were widowed at older ages, for both men and women, after controlling for covariates. Household income and health-related factors partially accounted for the positive association between age at widowhood and cognitive function. Those who divorced at younger ages also had lower cognitive function than their counterparts who divorced at older ages, but this association was only present among men, not women. Health-related factors partially accounted for the association between age at divorce and cognitive function among men. Findings highlight the importance of considering the role of timing of marital loss in cognitive health among older adults.