Nitrite ion is an ambident nucleophile, which can attack through ‘O’ or ‘N’.
Both nitrogen and oxygen are capable of donating electron pair. C – N bond, being stronger than N – O bond, attack occurs through C atom from alkyl halide forming nitroalkane.
However, sodium nitrite (NaNO2) is an ionic compound and oxygen is free to donate pair of electrons. Hence, attack occurs through oxygen resulting in the formation of alkyl nitrite.