Objective. To explore the effect of chronic heart failure complicated with type 2 diabetes mellitus on cognitive function in the elderly. Methods. 600 patients with chronic heart failure were selected from January 2018 to January 2021. All patients were divided into observation group (A) and control group (B). A was chronic heart failure complicated with type 2 diabetes mellitus group. B was chronic heart failure group. The clinical effects of the two groups were observed. Results. Compared with the clinical indexes during and after operation, there were differences in operation time, postoperative recovery time, and treatment cost between A and B, but the difference is not significant (all
P
>
0.05
). LVEF cardiac function index score, LVEF score of A compared with B, the difference was statistically significant (
P
<
0.05
). The MMSE score and MoCA score of the two groups were compared. Before operation, the MMSE score and MoCA score of A were lower than those of B, and the difference was statistically significant (
P
<
0.05
). After operation, the MMSE score and MoCA score in B were significantly higher than those in B, but the increasing trend of MMSE score and MoCA score in B was significantly higher than that in A (
P
<
0.05
). Comparison of HAMA score and HAMD score: before operation, the HAMA score and HAMD score were higher in A, but the difference is not significant (
P
>
0.05
). After operation, the scores of HAMA and HAMD in A and B decreased significantly, but the difference is not significant (
P
>
0.05
). Multivariate analysis showed that the fasting blood glucose and glycosylated hemoglobin were the risk factors of cognitive impairment. Conclusion. Type 2 diabetes mellitus in elderly patients with chronic heart failure will further aggravate cognitive impairment, and type 2 diabetes is an important independent risk factor affecting cognitive function, which accelerates cognitive impairment and significantly reduces the executive ability of elderly patients with chronic heart failure, resulting in a significant decline in patients’ ability to understand and apply information.