CCD photometry and MERLIN observations of M87 (Virgo A) have been used to study the variations of the radio to optical spectral index along the jet. In general, the radio spectrum is flatter than the optical one and both become steeper with distance from the nucleus. The optical fluxes of the bright knots lie at or below the extrapolation of the radio spectrum. These properties can be explained by power-law spectra at low frequencies that turn down gradually beyond a cut-off frequency. The location of the local cut-off frequency is estimated by fitting the measured fluxes to the theoretical spectrum of a truncated power-law electron energy distribution. The results are interpreted in terms of models of synchrotron emission from shock waves in extragalactic jets.