We present ab initio density functional theory studies for stoichiometric as well as nonstoichiometric amorphous silicon nitride, varying the stoichiometry between SiN and SiN. Stoichiometric amorphous SiN possesses the same local structure as crystalline SiN, with Si being fourfold coordinated and N being threefold coordinated. Only few Si-Si and N-N bonds and other defects are found in stoichiometric silicon nitride, and the electronic properties are very similar to the crystalline bulk. In over-stoichiometric SiN, the additional N results in N-N bonds, whereas in under-stoichiometric SiN the number of homopolar Si-Si bonds increases with decreasing N content. Analysis of the structure factor and the local coordination of the Si atoms indicates a slight tendency towards Si clustering, although at the investigated stoichiometries, phase separation is not observed. In the electronic properties, the conduction-band minimum is dominated by Si states, whereas the valence-band maximum is made up by lone pair N states. Towards Si rich samples, the character of the valence-band maximum becomes dominated by Si states corresponding to Si-Si bonding linear combinations. Adding small amounts of hydrogen, as typically used in passivating layers of photovoltaic devices, has essentially no impact on the overall structural and electronic properties.