Here's why:
* Ionic compounds are formed by the electrostatic attraction between positively charged ions (cations) and negatively charged ions (anions).
* Magnesium (Mg) is a metal and tends to lose two electrons to form a +2 cation (Mg²⁺).
* Oxygen (O) is a nonmetal and tends to gain two electrons to form a -2 anion (O²⁻).
* The opposite charges of the Mg²⁺ and O²⁻ ions attract each other, forming a strong ionic bond and creating the compound MgO.