Limited studies have investigated the influence of body mass index (BMI) trajectories on lung function covering the entire growth period. We conducted a prospective study utilizing data from the Swedish BAMSE birth cohort. Latent class mixture modelling was employed to examine the diversity in BMI z-scores from birth to 24 years of age. Participants with four or more BMI z-scores were included (n=3204, 78·4%). Pre-bronchodilator (BD) spirometry was tested at 8, 16, and 24 years, while post-BD spirometry, multiple-breath nitrogen washout (for lung clearance index, LCI), and urinary metabolomics data were assessed at 24 years. Six distinct BMI development groups were identified. Compared to the stable normal BMI group, the accelerated increasing BMI group exhibited reduced pre- and post-BD FEV1/FVC ratio z scores (β=-0·26, 95% CI=[-0·44, -0·08], and -0·22, [ -0·39, -0·05], respectively), along with elevated LCI (0·30, [0·22, 0·42]) at 24 years. The persistent high BMI group demonstrated lower FEV1, and FVC z scores growth between 16 and 24 years (-0.24, [-0.42, -0.05], and -0.27, [-0.45, -0.01], respectively), and elevated LCI (0·20, [0·03, 0·39]) at 24 years. However, those impairments were not observed in the accelerated resolving BMI group. Conversely, the persistent low BMI group displayed persistently decreased FEV1, and FVC from 8 to 24 years, as well as decreased lung function growth. Additionally, histidine-related metabolites were associated with pre- and post-BD FEV1 (hypergeometric FDR=0.008 and <0.001, respectively). Early interventions aiming for normal BMI during childhood may contribute to improved lung health later in life.