BackgroundSeveral studies have explored the association between temperature and cognitive function. However, few studies have examined the effect of extreme temperature on cognitive function. In this study, we aimed to quantify the long‐term effect of extreme temperature (e.g., heat waves, cold spells, and hot night excess (HNE)) on cognitive performance in middle‐aged and older people in China.MethodWe investigated 7915 aged >45 years people from the China Health and Retirement Longitudinal Study (CHARLS), surveyed in 2011 and 2015. A structured questionnaire was utilized to assess cognitive function, including four dimensions: episodic memory, attention, orientation, and visuo‐construction. Hourly ambient temperature from the ERA5‐Land datasets were used to calculate variables indicating extreme temperature. We performed difference‐in‐difference (DID) models to assess the potential causal relationship between extreme temperature and cognitive function.ResultsNon‐linear analyses suggested that both sustained increases in temperature and excessive variability in temperature increased the risk of cognitive decline. Meanwhile, we observed the extra risk of global cognitive function decline was 2.3% (95% Confidence interval (95% CI): 0.2%, 4.4%) for heat waves (one unit increase) and 5.9% (95% CI: 0.6%, 11.6%) for HNE (one unit increase), while the association for cold spells was insignificant. Two cognitive dimensions, episodic memory and visuo‐construction, were sensitive to these two heat‐related factors.ConclusionExtreme temperature was inversely related to cognitive performance in middle‐aged and older adults, which was substantial for heat waves and HNE particularly. The effect size varied by cognitive dimensions.