The association between coffee consumption and constipation remains unclear. This study aimed to examine the relationship of coffee consumption with the risk of constipation, while also investigating potential effect modifiers. This cross-sectional study included 7844 participants from the National Health and Nutrition Examination Survey (NHANES) 2007-2010. Coffee consumption was extracted from the 24-hour dietary recall. Constipation was assessed using the Bristol Stool Form Scale. The association between coffee consumption and constipation was assessed using multivariable restricted cubic spline and logistic regression with odds ratio (OR) and 95% confidence interval (CI). There was a J-shaped relationship between total coffee consumption and the risk of constipation in the whole population (p for nonlinearity = 0.049), with 1-2 cups/day of total coffee potentially reducing the risk of constipation by 39% (OR 0.61, 95% CI 0.35-1.06, p = 0.07). As for caffeinated coffee, a J-shaped association between its consumption and the risk of constipation was also observed in the whole population (p for nonlinearity = 0.008), with 1-2 cups/day being significantly associated with a reduced risk (OR 0.57, 95% CI 0.35-0.95, p = 0.03). When restricting to never drinkers of alcohol, the associations between total and caffeinated coffee consumption and constipation shifted to inverse linear trends, where at least 3 cups/day was significantly associated with an 88% reduction in constipation risk (total coffee: OR 0.12, 95% CI 0.02-0.68, p = 0.02; caffeinated coffee: OR 0.12, 95% CI 0.02-0.70, p = 0.02). Decaffeinated coffee showed no association with constipation. Consuming 1-2 cups of caffeinated coffee daily was associated with a reduced risk of constipation in the general population. Among never drinkers of alcohol, a linear protective effect was observed, with a notable 88% reduction in constipation risk for those consuming at least 3 cups per day. Moderate caffeinated coffee intake may therefore be a viable dietary strategy for managing constipation in the general population.