Children's intellectual development is influenced by both genetic inheritance and environmental experiences. Breastfeeding is one of the earliest such postnatal experiences. Breastfed children attain higher IQ scores than children not fed breast milk, presumably because of the fatty acids uniquely available in breast milk. Here we show that the association between breastfeeding and IQ is moderated by a genetic variant in FADS2, a gene involved in the genetic control of fatty acid pathways. We confirmed this gene-environment interaction in two birth cohorts, and we ruled out alternative explanations of the finding involving gene-exposure correlation, intrauterine growth, social class, and maternal cognitive ability, as well as maternal genotype effects on breastfeeding and breast milk. The finding shows that environmental exposures can be used to uncover novel candidate genes in complex phenotypes. It also shows that genes may work via the environment to shape the IQ, helping to close the nature versus nurture debate.