Theory:

You have seen how metals react with a variety of chemicals in the activities above. What causes metals to react the way they do? Let us review what we have learnt in Class IX about the electronic configuration of elements. Noble gases, which have a completely filled valence shell, have little chemical activity. As a result, we describe element reactivity as a tendency to obtain a completely filled valence shell.

Consider the electronic configuration of noble gases, as well as various metals and non-metals.

 Type of element Element Symbol Atomic number Number of electrons in shellsK       L       M       N Noble gases Helium NeonArgon $$He$$$$Ne$$$$Ar$$ $$2$$$$10$$$$18$$ $$2$$     $$2$$        $$8$$$$2$$        $$8$$        $$8$$ Metals SodiumMagnesiumAluminium PotassiumCalcium $$Na$$$$Mg$$$$Al$$$$K$$$$Ca$$ $$11$$$$12$$$$13$$$$19$$$$20$$ $$2$$        $$8$$        $$1$$$$2$$        $$8$$        $$2$$$$2$$        $$8$$        $$3$$$$2$$        $$8$$        $$8$$        $$1$$$$2$$        $$8$$        $$8$$        $$2$$ Non-metals NitrogenOxygenFluorinePhosphorusSulphurChlorine $$N$$$$O$$$$F$$$$P$$$$S$$$$Cl$$ $$7$$$$8$$$$9$$$$15$$$$16$$$$17$$ $$2$$        $$5$$$$2$$        $$6$$$$2$$        $$7$$$$2$$        $$8$$        $$5$$$$2$$        $$8$$        $$6$$$$2$$        $$8$$        $$7$$

A sodium atom has one electron in its outermost shell, as shown in the table. Its L shell becomes the outermost shell and has a stable octet if it loses one electron from its M shell. The nucleus of this atom still retains $$11$$ protons, but the number of electrons has dropped to $$10$$, resulting in a net positive charge, leading to the sodium cation $$Na^+$$. On the other hand, chlorine contains seven electrons in its outermost shell.

 Sodium atom Chlorine atom Electrons present = $$11$$Electronic configuration = ($$2, 8, 1$$) Electrons present = $$17$$Electronic configuration = ($$2, 8, 7$$)

Where, sodium will donate $$1$$ electron to the chlorine atom to complete their respective octets. In this way, the molecule sodium chloride ($$NaCl$$), an ionic compound will form.

The reaction is as shown below:

$\begin{array}{l}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\mathit{Na}\phantom{\rule{0.147em}{0ex}}\stackrel{}{⟶}{\phantom{\rule{0.147em}{0ex}}\mathit{Na}}^{+}+{e}^{-}\\ \left(2,8,1\right)\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\left(2,8\right)\\ \phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\mathit{Sodium}\phantom{\rule{0.147em}{0ex}}\mathit{cation}\\ \mathit{Cl}+{e}^{-}\stackrel{}{⟶}\phantom{\rule{0.147em}{0ex}}{\mathit{Cl}}^{-}\\ \left(2,8,7\right)\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\left(2,8,8\right)\\ \phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\mathit{Chlorine}\phantom{\rule{0.147em}{0ex}}\mathit{anion}\\ {\mathit{Na}}^{+}+\phantom{\rule{0.147em}{0ex}}{\mathit{Cl}}^{-}\stackrel{}{⟶}\phantom{\rule{0.147em}{0ex}}\mathit{NaCl}\\ \phantom{\rule{4.116em}{0ex}}\mathit{Sodium}\phantom{\rule{0.147em}{0ex}}\mathit{chloride}\end{array}$

Sodium chloride formation

Since sodium and chloride ions have opposite charges, they attract one other and are held together by strong electrostatic forces to form sodium chloride ($$NaCl$$). We should note that sodium chloride does not exist as molecules but aggregates of oppositely charged ions.
Common salt ($$NaCl$$) is an ionic compound that conducts electricity only in a molten state. In a molten state, the electrostatic forces of attraction between oppositely charged ions ($$Na^+$$ and $$Cl^–$$) are overcome due to heat. Hence, the ions move freely and conduct electricity.
Let's look at another example of ionic compound formation, magnesium chloride.

 Magnesium atom Chlorine atom Electrons present = $$12$$ Electronic configuration = ($$2, 8, 2$$) Electrons present = $$17$$Electronic configuration = ($$2, 8, 7$$)

The reaction is shown below:

$\begin{array}{l}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\mathit{Mg}\phantom{\rule{0.147em}{0ex}}\stackrel{}{⟶}{\phantom{\rule{0.147em}{0ex}}\mathit{Mg}}^{2+}+{\mathit{2e}}^{-}\\ \left(2,8,2\right)\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\left(2,8\right)\\ \phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\mathit{Magnesium}\phantom{\rule{0.147em}{0ex}}\mathit{cation}\\ \mathit{Cl}+{e}^{-}\stackrel{}{⟶}\phantom{\rule{0.147em}{0ex}}{\mathit{Cl}}^{-}\\ \left(2,8,7\right)\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\left(2,8,8\right)\\ \phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\phantom{\rule{0.147em}{0ex}}\mathit{Chlorine}\phantom{\rule{0.147em}{0ex}}\mathit{anion}\\ {\mathit{Mg}}^{2+}+2{\mathit{Cl}}^{-}\stackrel{}{⟶}\phantom{\rule{0.147em}{0ex}}{\mathit{MgCl}}_{2}\\ \phantom{\rule{4.557em}{0ex}}\mathit{Magnesium}\phantom{\rule{0.147em}{0ex}}\mathit{chloride}\end{array}$

$$MgCl_2$$ formation

Ionic compounds or electrovalent compounds are compounds formed by transferring electrons from a metal to a non-metal in this way.
Are you able to identify the cation and anion in $$MgCl_2$$?

The answer for this question is "yes".

An anion is a negatively charged atom or molecule, while a cation is a positively charged atom or molecule. Thus, $$Mg^{2+}$$ is known as cation and $$Cl^-$$ is known as anion.
Reference:
https://chem.libretexts.org/@api/deki/files/78198/CK12_Screenshot_8-6-2.png?revision=1&size=bestfit&width=262&height=126