Sodium has one valence electron, which it readily gives up to achieve a stable electron configuration like noble gases. Chlorine has seven valence electrons and needs one more electron to complete its stable octet.
When sodium comes in contact with chlorine, the sodium atom transfers its single valence electron to the chlorine atom, resulting in the formation of positively charged sodium ion (Na+) and a negatively charged chloride ion (Cl-). The electrostatic attraction between these oppositely charged ions holds the compound together, forming sodium chloride (NaCl), a stable ionic compound.
This 1:1 ratio of sodium and chlorine atoms is necessary to maintain electrical neutrality. Each sodium atom loses one electron, and each chlorine atom gains one electron, so the overall charge of the compound remains neutral.