Electrons have an intrinsic, indivisible, magnetic dipole aligned with their internal angular momentum (spin). The magnetic interaction between two electronic spins can therefore impose a change in their orientation. Similar dipolar magnetic interactions exist between other spin systems and have been studied experimentally. Examples include the interaction between an electron and its nucleus and the interaction between several multi-electron spin complexes1, 2, 3, 4, 5. The challenge in observing such interactions for two electrons is twofold. First, at the atomic scale, where the coupling is relatively large, it is often dominated by the much larger Coulomb exchange counterpart1. Second, on scales that are substantially larger than the atomic, the magnetic coupling is very weak and can be well below the ambient magnetic noise. Here we report the measurement of the magnetic interaction between the two ground-state spin-1/2 valence electrons of two 88Sr+ ions, co-trapped in an electric Paul trap. We varied the ion separation, d, between 2.18 and 2.76 micrometres and measured the electrons’ weak, millihertz-scale, magnetic interaction as a function of distance, in the presence of magnetic noise that was six orders of magnitude larger than the magnetic fields the electrons apply on each other. The cooperative spin dynamics was kept coherent for 15 seconds, during which spin entanglement was generated, as verified by a negative measured value of −0.16 for the swap entanglement witness. The sensitivity necessary for this measurement was provided by restricting the spin evolution to a decoherence-free subspace that is immune to collective magnetic field noise. Our measurements show a d−3.0(4) distance dependence for the coupling, consistent with the inverse-cube law.