Heavy metals are neurotoxic, associated with brain dysfunction, and have been linked with cognitive decline in adults. This study was aimed to characterize chronic exposure to metals (Cd, Be, Co, Hg, Sn, V, Al, Ba, Cr, Cu, Fe, Li, Mn, Ni, Pb, and Zn) and metalloids (As, B, Sb) and assess its impact on cognitive performance of Tehran's residents, capital of Iran. Scalp hair samples gathered from 200 volunteered participants (110 men and 90 women), aged 14-70 years and quantified by inductively coupled plasma atomic emission spectroscopy (ICP-OES). Attention and executive function, two measures of cognitive performance, were characterized using the trail making test (TMT) part A and B, respectively. Mental flexibility was characterized as the Delta TMT B-A scores and cognitive efficiency or dissimulation as the ration between TMT B and A scores. A comprehensive questionnaire was used to gather information on demographic and socioeconomic as well as lifestyle and health status. The highest and lowest mean concentrations were observed for B (325 μg/g) and As (0.29 μg/g), respectively. Results indicated that chronic metal exposure measured in hair changed significantly based on gender and age (p < 0.05). The levels of Cr, Fe, Ni, Si, Hg, Pb and B were significantly higher in males' hair, whereas those of Ag and Ba were greater in females' hair (p < 0.05). The results of the cognitive TMT test were significantly different between gender and age groups (p < 0.05). Moreover, results revealed that As, Hg, Mn, and Pb levels in hair were significantly associated with poorer participants' performance scores in the TMT test (p < 0.05). Age, gender, cigarette smoking, water-pipe smoking, traffic density in the area of residence, and dental amalgam filling were significant factors affecting the TMT test scores. The results suggest that chronic exposure to metals has detrimental effects on attention, executive function, mental flexibility and cognitive efficiency.