The overall or net charge of any ionic compound must equal zero. Although ions themselves are either positive (cation) or negative (anion) by definition, the only way they can bond together and form a compound is by seeking an opposite charge.
An ionic compound in chemistry refers to any chemical compound in which ions are held together, or bound, through ionic bonds. Ionic bonds, by definition, are electrostatic attractions between two ions with an opposite charge. In other words, an ionic bond can only form between a cation and an anion.
Although there is no limit to the number of ions contained in an ionic compound, in order for the compound to remain whole, the net charge needs to equal zero. Therefore, there must be an equal number of cations and anions within the completed compound.
One popular, and common, example of an ionic compound which displays this principle is table salt or sodium chloride, which has the chemical formula NaCl. The formation of table salt is the result of a ionic bond between a sodium cation, Na+ and a chlorine anion, Cl-. The structure of each ion is different than an atom of the same element (Na or Cl) that holds a neutral charge due to an equal number of protons and electrons. It is only once each atom becomes an ion that is it able to bond with the other atom and create an ionic compound.