In plumbing, a trap is a U-shaped portion of pipe designed to trap liquid or gas to prevent unwanted flow; most notably sewer gases from entering buildings while allowing waste materials to pass through. In oil refineries, traps are used to prevent hydrocarbons and other dangerous gases and chemical fumes from escaping through drains. In heating systems, the same feature is used to prevent thermo-siphoning which would allow heat to escape to locations where it is not wanted. Similarly, some pressure gauges are connected to systems using U bends to maintain a local gas while the system uses liquid. For decorative effect, they can be disguised as complete loops of pipe, creating more than one U for added efficacy.
In domestic applications, traps are typically U, S, Q, or J-shaped pipe located below or within a plumbing fixture. An S-shaped trap is also known as an S-bend. It was invented by Alexander Cumming in 1775 but became known as the U-bend following the introduction of the U-shaped trap by Thomas Crapper in 1880. The U-bend could not jam, so, unlike the S-bend, it did not need an overflow. In the United States, traps are commonly referred to as P-traps. It is the addition of a 90 degree fitting on the outlet side of a U-bend, thereby creating a P-like shape (oriented horizontally). It is also referred to as a sink trap because it is installed under most sinks.
Because of its shape, the trap retains some water after the fixture's use. This water creates an air seal that prevents sewer gas from passing from the drain pipes back into the building. Essentially all plumbing fixtures including sinks, bathtubs, and showers must be equipped with either an internal or external trap. Toilets almost always have an internal trap.
Because it is a localized low-point in the plumbing, sink traps also tend to capture small and heavy objects (such as jewellery or coins) accidentally dropped down the sink. Traps also tend to collect hair, sand, food waste and other debris and limit the size of objects that enter the plumbing system, thereby catching oversized objects. For all of these reasons, most traps may be disassembled for cleaning or provide a cleanout feature.
Where a volume of water may be rapidly discharged through the trap, a vertical vented pipe called a standpipe may be attached to the trap to prevent the disruption of the seal in other nearby traps. The most common use of standpipes in houses is for clothes washing machines, which rapidly dispense a large volume of wastewater while draining the wash and rinse cycles.
In chemical engineering applications, a trap may be known as a lute. 
An S-shaped trap is also known as an S-bend. It was invented by Alexander Cumming in 1775 but became known as the U-bend following the introduction of the U-shaped trap by Thomas Crapper in 1880. The new U-bend could not jam, so, unlike the S-bend, it did not need an overflow.
Once invented, despite being simple and reasonably reliable, widespread use was slow coming. In Britain, the requirement to use traps was introduced only after the Great Stink in London, in the summer of 1858, when the objectionable smell of the River Thames, which was effectively an open sewer, affected the nearby Houses of Parliament. That motivated the legislators to authorise the construction of a modern sewerage system in the city, of which the S-bend was an essential component. As of 2017[update], only about two-thirds of the world population have access to traps, in spite of the evidence that good sewage systems significantly improve economic productivity in places that employ them.
Maintaining the water seal is critical to trap operation; traps might dry out, and poor venting can suction or blow water out of the traps. This is usually avoided by venting the drain pipes downstream of the trap; by being vented to the atmosphere outside the building, the drain lines never operate at a pressure much higher or lower than atmospheric pressure. In the United States, plumbing codes usually provide strict limitations on how far a trap may be located from the nearest vent stack.
When a vent cannot be provided, an air admittance valve may be used instead. These devices avoid negative pressure in the drain pipe by venting room air into the drain pipe (behind the trap). A "Chicago Loop" is another alternative.
When a trap is installed on a fixture that is not routinely used—such as a floor drain—the eventual evaporation of the water in the trap must be considered. In these cases, a trap primer may be installed; these are devices that automatically recharge traps with water to maintain their water seals.
In some regions of the US, "S" traps are no longer accepted by the building codes as the unvented S-traps tend to siphon dry. It may be possible to determine whether a household uses an S- or U-bend by the presence of an overflow pipe outlet.[clarification needed] What is required instead is a P-trap with proper venting. Certain drum-styled traps are also discouraged or banned.