Ayer is a town in Middlesex County, Massachusetts, United States. The town's population was 7,427 at the 2010 census.According to the United States Census Bureau, the town has a total area of 9.6 square miles (24.8 km2), of which 9.0 square miles (23.4 km2) is land and 0.6 square mile (1.4 km2) (5.75%) is water.
Ayer's history dates back to 1667, when the first mill in the agricultural community was built. The settlement sits on what the Nipmuc Indianscalled Nainacocius. A brook remains with that name. Originally part ofGroton, the community was initially called Groton Junction or South Groton. The town of Ayer was incorporated in 1871, and was named in honor of Dr. James Cook Ayer, a prominent resident of Lowell who provided the funding for the construction of the Town Hall.