When sodium and chlorine react, each sodium atom gives its single outer electron to a chlorine atom. The sodium atom becomes a sodium ion, \mathrm{Na^+}, and the chlorine atom becomes a chloride ion, \mathrm{Cl^-}.
Both ions now have the same arrangement of electrons as a noble gas: \mathrm{Na^+} like neon and \mathrm{Cl^-} like argon.
The positive and negative ions attract each other by electrostatic force. This attraction between oppositely charged ions is called an ionic bond, and it usually forms between a metal and a nonmetal.
The attraction between two ions is stronger when their charges are larger and when they are closer together.
In magnesium oxide, the ions \mathrm{Mg^{2+}} and \mathrm{O^{2-}} carry twice the charge of \mathrm{Na^+} and \mathrm{Cl^-}, and they are also smaller, so they sit closer together. The attraction between them is therefore much stronger.
This is why sodium chloride melts at 801 °C, while magnesium oxide melts at about 2800 °C.
An ionic compound has no overall charge, so its ions combine in the ratio that makes the total positive charge equal to the total negative charge.
One \mathrm{Mg^{2+}} ion balances two \mathrm{Cl^-} ions, giving \mathrm{MgCl_2}. Two \mathrm{Al^{3+}} ions balance three \mathrm{O^{2-}} ions, giving \mathrm{Al_2O_3}.
The formula shows only the simplest whole-number ratio of the ions, because an ionic crystal is not made of separate molecules. A polyatomic ion is treated as a single unit, as in calcium carbonate, \mathrm{CaCO_3}.
In an ionic crystal, each ion is surrounded by ions of the opposite charge and held firmly in place, so ionic crystals are hard and have high melting points.
If a strong force shifts one layer of ions against the next, ions of the same charge come face to face.
These like charges repel each other, and the crystal splits cleanly along the layer. This is why ionic crystals are brittle.
Solid ionic compounds do not conduct electricity, because their ions are fixed in place and cannot move.
When an ionic compound is melted or dissolved in water, its ions are free to move. Positive ions move towards the negative electrode and negative ions towards the positive electrode, so a current flows.
A substance that conducts electricity in this way when it is molten or dissolved is called an electrolyte.