Formula from Chapter 3: chemical amount from mass of a pure substance
\(n_{\mathrm{\ce{compound}}} = \dfrac{m_{\mathrm{\ce{compound}}}}{M_{\mathrm{\ce{compound}}}}\)
You can use this if: you know the chemical formula of the compound
\(M_{\mathrm{\ce{MgCl2}}}\) \(= M_{\mathrm{\ce{MgCl2}}}\) molar mass (one formula unit of \(\ce{MgCl2}\) contains one magnesium atom and two chloride atoms)
\(\ \ \ =95.21\ \frac{\mathrm{g}}{\mathrm{mol}}\)
\(m_{\mathrm{\ce{MgCl2}}}\) \(= 4.0\ \mathrm{g}\) about a teaspoon of \(\ce{MgCl2}\)
\(n_{\mathrm{\ce{MgCl2}}}\) \(= \dfrac{m_{\mathrm{\ce{MgCl2}}}}{M_{\mathrm{\ce{MgCl2}}}}\)
\(\ \ \ =\dfrac{4.0\ \mathrm{g}}{95.21\ \frac{\mathrm{g}}{\mathrm{mol}}}\)
\(\ \ \ =0.042\ \mathrm{mol}\)