A theory is presented of the elongation of double-stranded DNA confined in a nanochannel based on a study of the formation of hairpins. A hairpin becomes constrained as it approaches the wall of a channel which leads to an entropic force causing the hairpin to tighten. The DNA in the hairpin remains double-stranded. The free energy of the hairpin is significantly larger than what one would expect if this entropic effect were unimportant. As a result, the distance between hairpins or the global persistence length is often tens of micrometer long and may even reach millimeter sizes for 10 nm thin channels. The hairpin shape and size and the DNA elongation are computed for nanoslits and circular and square nanochannels. A comparison with experiment is given.