Objective: The incidence of myocardial infarction (MI) has decreased in general but not among younger middle-aged adults. We performed a cohort study of the association between neighbourhood socioeconomic status (SES) at the age of 40 and risk of MI before the age of 50 years. Methods: All individuals in Sweden were included in the year of their 40th birthday, if it occurred between 1998 and 2010. National registers were used to categorise neighbourhood SES into high, middle and low, and to retrieve information on incident MI and coronary heart disease (CHD). Cox regression models, adjusted for marital status, education level, immigrant status and region of residence, provided an estimate of the HRs and 95% CIs for MI or CHD. Results: Out of 587 933 men and 563 719 women, incident MI occurred in 2877 (0.48%) men and 932 (0.17%) women; and CHD occurred in 4400 (0.74%) men and 1756 (0.31%) women during a mean follow-up of 5.5 years. Using individuals living in middle-SES neighbourhoods as referents, living in high-SES neighbourhoods was associated with lower risk of MI in both sexes (HR (95% CI): men: 0.72 (0.64 to 0.82), women: 0.66 (0.53 to 0.81)); living in low-SES neighbourhoods was associated with a higher risk of MI (HR (95% CI): men: 1.31 (1.20 to 1.44), women: 1.28 (1.08 to 1.50)). Similar risk estimates for CHD were found. Conclusions: The results of our study suggest an increased risk of MI and CHD among residents from low-SES neighbourhoods and a lower risk in those from high-SES neighbourhoods compared with residents in middle-SES neighbourhoods.