Answer:
To gain stability.
Explanation:
Atoms are stable when they have complete octet means they have 8 valance electrons.
The halogens elements are present in group seventeen. All atoms of halogen elements have seven valance electrons. They need one more electron to complete the octet and get stable.
That's why all halogens atoms are very reactive because they react with other atoms complete the octet and present in the form of diatomic molecule.
The halogen atoms react with metals and form ionic compound.
For example:
Sodium chloride is ionic compound. The electronegativity of chlorine is 3.16 and for sodium is 0.93. There is large difference is present. That's why electron from sodium is transfer to the chlorine. Sodium becomes positive and chlorine becomes negative ion. Both atoms are combine by electrostatic attraction.