Here's why:
* Metals tend to lose electrons because they have relatively few electrons in their outermost shell. This makes them electropositive and they want to achieve a stable, full outer shell by giving away these electrons.
* Nonmetals, on the other hand, have many electrons in their outer shell and want to gain electrons to fill it and achieve stability. This makes them electronegative.
When a metal and nonmetal react, the metal atom will lose electrons and become a positively charged ion (cation), while the nonmetal atom will gain electrons and become a negatively charged ion (anion). These oppositely charged ions then attract each other, forming an ionic compound.
Example:
Sodium (Na), a metal, reacts with chlorine (Cl), a nonmetal, to form sodium chloride (NaCl).
* Sodium loses one electron to become Na⁺.
* Chlorine gains one electron to become Cl⁻.
* The oppositely charged ions then attract each other, forming the ionic compound NaCl.