Ionic bonding occurs when atoms of metals and non-metals combine to form ionic compounds. Atoms of metals will donate electrons to form cations, while atoms of non-metals will accept electrons to form anions. This transfer of electrons allows the atoms to achieve stable electron configurations similar to noble gases. Common examples are sodium chloride, which forms when sodium donates an electron to chlorine, and magnesium oxide, which forms when magnesium donates two electrons to oxygen. The chemical formulas of ionic compounds are written to balance the charges of the cation and anion.