Magnesium chloride (MgCl2) is an ionic compound formed by the transfer of electrons from magnesium atoms to chlorine atoms. Magnesium loses two electrons to form Mg2+ ions, while each chlorine atom gains one electron to form Cl- ions. The electrostatic attraction between the positively charged Mg2+ ions and the negatively charged Cl- ions holds the compound together.