To add to the above answer, the reason this compound is ionic is due to the transfer of the electrons which is due to the difference in the electronegativities of both elements.
Electronegativity is the attraction of one atom's nucleus (protons) for another atom's valence electrons in a bond. It is similar to a tug-of-war between two atoms and the valence electrons. The element with the strongest electronegativity value wins the tug-of-war. In this compound, sodium has an electronegativity value of 0.9 while chlorine has an electronegativity value of 3.0. This means that chlorine would attract the valence electron of sodium's because chlorine's nucleus attracts that electron more strongly than sodium does.
The electron transfers to chlorine which completes an octet (8 valence electrons) in chlorine and becomes the chloride ion with a -1 charge. Sodium has lost its only valence electron which leaves an octet for sodium as well and creates a +1 charge. Since opposite charges attract, the sodium ion and chloride ion attract and form a bond.
Sodium chloride or NaCl is an ionic compounds made up of sodium (Na) and Chloride (Cl) ions.
An atom can lose an electron and attain a positive charge to become a cation. Similarly, an atom can gain an electron and become negatively charged to become an anion. When an anion and cation interact, they balance each other. In other words, in an ionic compound, an atom loses an electron, while the other atom gains it. In case of sodium chloride, sodium atom is the cation (`Na^+` ) and loses an electron, while chlorine atom gains that electron and becomes the negatively charged anion (`Cl^-` ). Hence sodium chloride is the ionic compound made by electron exchange between two different ions.
Hope this helps.