Background: Body mass index (BMI) levels have been proven to have a significant relationship with depression. However, there is limited evidence regarding the impact of BMI trajectories and the average annual changes among adults aged 40-74 years on the risk of developing depression. Methods: In this cross-sectional study, participants aged 40-74 were retrieved from the National Health and Nutrition Examination Survey (NHANES) 2005-2018. The latent class trajectory model (LCTM) was employed to identify BMI trajectories. Depression was evaluated using the validated Patient Health Questionnaire (PHQ-9). Multinomial logistic regression models were conducted to explore the associations between BMI trajectories, its average annual changes and depression. Results: A total of 17, 171 participants were included. Five BMI trajectories were identified and labeled as the slight increase trajectory (n = 7830, 45.60 %), stable trajectory (n = 5605, 32.64 %), moderate increase trajectory (n = 2562, 14.92 %), rapid increase trajectory (n = 685, 3.99 %) and increase-to-decrease trajectory (n = 489, 2.85 %), respectively. Compared to individuals with stable trajectory, those who experienced a rapid increase trajectory (OR = 2.39, 95%CI 1.72-3.34) and an increase-to-decrease trajectory (OR = 2.07, 95%CI 1.51-2.84) had a higher risk of developing depression. During early adulthood, absolute (OR = 1.46, 95%CI 1.23-1.73) and relative (OR = 1.47, 95%CI 1.25-1.71) average annual BMI growths were associated with depression. In recent adulthood, average annual BMI loss was shown to increase the risk of developing depression. Conclusions: This study highlights an increased risk of depression among participants aged 40-74 years with nonstable BMI trajectories, particularly among females. These findings emphasize the importance of maintaining a stable weight to reduce the risk of depression, especially in middle-aged and older adults.