On adding $\ce{NaCN}$ to $\ce{RX}$, we get
$$\ce{NaCN + RX -> RCN + NaX}$$
And not $\ce{RNC}$. Clayden gives the explanation as follows:
Although linear cyanide (which is isoelectronic with $\ce{N2}$) has a lone pair on nitrogen and a lone pair on carbon, the nucleophilic atom is usually anionic carbon rather than neutral nitrogen as the $\mathrm{sp}$ orbital on carbon is of higher energy than that on the more electronegative nitrogen, and therefore constitutes the HOMO.
So according to Clayden, Carbon being less electronegative, has a higher HOMO than Nitrogen, thus it's lone pair attack the LUMO, to form a bond as the less the difference in energy between HOMO and LUMO, the stronger is the bond
But when we consider the same case with $\ce{NaNO2}$, so according to Clayden, nitrogen being less electronegative than oxygen, Nitrogen should have a higher energy HOMO and thus in a nucleophilic substitution reaction it is from the nitrogen side, that the reaction should take place but ... $$\ce{NaNO2 + RX -> RONO + NaX}$$
The reaction takes place from the oxygen side. Why?
Also when we have the silver salts of them, e.g., $\ce{AgCN}$, the reaction becomes: $$\ce{AgCN + RX -> RNC + AgX}$$ Similarly, $$\ce{AgNO2 + RX -> RNO2 + AgX}$$
Why does it change?