We study the electronic structures and dielectric functions of the simple hydrides LiH, NaH, MgH2 and AlH3, and the complex hydrides Li3AlH6, Na3AlH6, LiAlH4, NaAlH4 and Mg(AlH4)2, using first principles density functional theory and GW calculations. All these compounds are large gap insulators with GW single particle band gaps varying from 3.5 eV in AlH3 to 6.5 eV in the MAlH4 compounds. The valence bands are dominated by the hydrogen atoms, whereas the conduction bands have mixed contributions from the hydrogens and the metal cations. The electronic structure of the aluminium compounds is determined mainly by aluminium hydride complexes and their mutual interactions. Despite considerable differences between the band structures and the band gaps of the various compounds, their optical responses are qualitatively similar. In most of the spectra the optical absorption rises sharply above 6 eV and has a strong peak around 8 eV. The quantitative differences in the optical spectra are interpreted in terms of the structure and the electronic structure of the compounds.