It depends on the number of valence electrons required to make octet or duplet( in case of H). For example, Nitrogen(atomic number = 7) has electronic configuration(2,5) which means nitrogen has 5 valence electrons and requires 3 more electrons to complete its octet. After gaining 3 electrons from atoms of an element with less electronegativity than N, it forms nitride ion ([tex]N ^{-3} [/tex]). Hope this helps.