Compounds can form from two nonmetals by sharing their electrons in a covalent manner.
In a covalent bond, atoms share pairs of electrons in order to achieve a full outer shell, which stabilizes the atoms involved. This type of bonding typically occurs between nonmetals, which have similar electronegativities and an affinity for electrons. In contrast, ionic bonds arise from the transfer of electrons from one atom to another, typically between a metal and a nonmetal, while metallic bonds involve a ‘sea of electrons’ that are shared among a lattice of metal atoms. Cation exchange is a different process that occurs in certain types of chemical reactions but does not pertain to the formation of stable compounds from nonmetals.