Metal (M) is from Group 1 of the periodic table (alkali metals).
Alkali metals have one valence electron ($ns^1$). They tend to lose this electron to form a cation with a +1 charge (M$^+$).
Example: Na $\rightarrow$ Na$^+$ + e$^-$
Non-metal (X) is from Group 16 of the periodic table (chalcogens).
Chalcogens have six valence electrons ($ns^2 np^4$). They need two more electrons to complete their octet ($ns^2 np^6$). They tend to gain two electrons to form an anion with a -2 charge (X$^{2-}$).
Example: O + 2e$^-$ $\rightarrow$ O$^{2-}$
When M and X react to form an ionic compound, the total positive charge must balance the total negative charge to achieve electrical neutrality.
Let the formula of the compound be M$_a$X$_b$.
The total positive charge is $a \times (+1) = +a$.
The total negative charge is $b \times (-2) = -2b$.
For neutrality, $+a = |-2b|$, so $a = 2b$.
The simplest whole number ratio for $a$ and $b$ that satisfies this is $a=2$ and $b=1$.
So, the formula of the compound is M$_2$X$_1$, which is written as M$_2$X.
Example: Sodium (Na, Group 1) reacts with Oxygen (O, Group 16) to form Sodium Oxide, Na$_2$O.
$2 \text{Na}^+ + \text{O}^{2-} \rightarrow \text{Na}_2\text{O}$.
Another example: Potassium (K, Group 1) reacts with Sulfur (S, Group 16) to form Potassium Sulfide, K$_2$S.
\[ \boxed{\text{M}_2\text{X}} \]