Excess bodyweight (BMI >25 kg/m2) in midlife (age 40-65 years) has been linked to future cognitive decline and an increased risk of dementia. Whether chronic exposure to excess bodyweight in the early decades of life (<40 years) is associated with compromised cognitive function by midlife, however, remains unclear. This study therefore aimed to test potential bidirectional direct and indirect pathways linking cumulative exposure to excess bodyweight and cognitive function in the early decades of life. In this longitudinal analysis, harmonised measures of BMI and cognitive function were available in 19 742 participants aged 47-53 years recruited to the 1946 National Survey of Health and Development (n=2131), the 1958 National Child Development Study (n=9385), and the 1970 British Cohort Study (n=8226). Individual BMI trajectories spanning three decades from age 10-40 years were created for each participant and excess bodyweight duration, BMI change between ages, and cumulative excess bodyweight exposure were calculated. Harmonised measures of verbal and non-verbal ability, mathematical ability, and reading ability were used to create a latent factor for childhood cognitive function, and immediate and delayed recall, animal naming, and letter-search speed tests were used for midlife cognitive function. Multivariable linear regression and structural equation models (SEM) were used to test for potential bidirectional relationships between cognition and excess bodyweight in both individual cohorts and pooled datasets while accounting for other potential early-life confounders. Increases in BMI during adolescence and greater cumulative exposure to excess bodyweight across early life were associated with lower midlife cognitive function in all cohorts (eg, pooled difference in cognitive function per 10 years excess bodyweight duration -0·10; 95% CI -0·12 to -0·08; p<0·001). Further adjustment for childhood cognitive function attenuated many of these associations towards the null (eg, pooled difference in cognitive function per 10 years excess bodyweight duration -0·04; 95% CI -0·06 to -0·02; p=0·001), however, with any remaining associations then fully attenuating once further adjusted for other early-life factors (eg, pooled difference in cognitive function per 10 years excess bodyweight duration 0, -0·03 to 0·01; p=0·38). In the reverse direction, low childhood cognition was associated with greater cumulative exposure to excess bodyweight over the next four decades, although much of this relationship was found to probably be explained via other potentially modifiable upstream early-life factors such as childhood disadvantage. SEM in all cohorts suggested the presence of modest direct and indirect pathways connecting earlier cognitive function to later excess bodyweight, but scarce evidence for an effect of early-life excess bodyweight on cognitive function by midlife. The association between cumulative exposure to excess bodyweight in early life and lower cognitive function in midlife is probably confounded by a persistently lower cognitive function from childhood. Initiatives to improve early-life factors such as childhood disadvantage and education, however, might exert dual but independent benefits on both of these factors before old age. Alzheimer's Research UK, Diabetes Research and Wellness Foundation, Diabetes UK, British Heart Foundation, and Medical Research Council.