Enter a chemical formula to calculate its molar mass.
// Define atomic masses for common elements
var atomicMasses = {
"H": 1.008, "He": 4.003, "Li": 6.941, "Be": 9.012, "B": 10.811, "C": 12.011, "N": 14.007, "O": 15.999, "F": 18.998, "Ne": 20.180,
"Na": 22.990, "Mg": 24.305, "Al": 26.982, "Si": 28.086, "P": 30.974, "S": 32.065, "Cl": 35.453, "K": 39.098, "Ar": 39.948, "Ca": 40.078,
"Sc": 44.956, "Ti": 47.867, "V": 50.942, "Cr": 51.996, "Mn": 54.938, "Fe": 55.845, "Ni": 58.693, "Co": 58.933, "Cu": 63.546, "Zn": 65.38,
"Ga": 69.723, "Ge": 72.63, "As": 74.922, "Se": 78.971, "Br": 79.904, "Kr": 83.798, "Rb": 85.468, "Sr": 87.62, "Y": 88.906, "Zr": 91.224,
"Nb": 92.906, "Mo": 95.96, "Tc": 98, "Ru": 101.07, "Rh": 102.91, "Pd": 106.42, "Ag": 107.87, "Cd": 112.41, "In": 114.82, "Sn": 118.71,
"Sb": 121.76, "I": 126.90, "Te": 127.60, "Xe": 131.29, "Cs": 132.91, "Ba": 137.33, "La": 138.91, "Ce": 140.12, "Pr": 140.91, "Nd": 144.24,
"Pm": 145, "Sm": 150.36, "Eu": 151.96, "Gd": 157.25, "Tb": 158.93, "Dy": 162.50, "Ho": 164.93, "Er": 167.26, "Tm": 168.93, "Yb": 173.05,
"Lu": 174.97, "Hf": 178.49, "Ta": 180.95, "W": 183.84, "Re": 186.21, "Os": 190.23, "Ir": 192.22, "Pt": 195.08, "Au": 196.97, "Hg": 200.59,
"Tl": 204.38, "Pb": 207.2, "Bi": 208.98, "Po": 209, "At": 210, "Rn": 222, "Fr": 223, "Ra": 226, "Ac": 227, "Pa": 231.04,
"Th": 232.04, "Np": 237, "U": 238.03, "Am": 243, "Pu": 244, "Cm": 247, "Bk": 247, "Cf": 251, "Es": 252, "Fm": 257,
"Md": 258, "No": 259, "Rf": 261, "Lr": 262, "Db": 262, "Bh": 264, "Sg": 266, "Mt": 268, "Rg": 272, "Hs": 277
};
// Recursive parser for chemical formulas, handles parentheses and subscripts
var parseFormulaRecursive = function(formulaString) {
var elements = {}; // Stores {element: count} for the current level
var i = 0; // Current position in the formula string, shared across recursive calls
var len = formulaString.length;
function parseGroup() {
var currentGroupElements = {};
while (i < len) {
var char = formulaString[i];
if (char === '(') {
i++; // Move past '('
var subGroupElements = parseGroup(); // Recursively parse subgroup
var subMultiplier = 1;
if (i < len && /\d/.test(formulaString[i])) {
var numStr = '';
while (i < len && /\d/.test(formulaString[i])) {
numStr += formulaString[i];
i++;
}
subMultiplier = parseInt(numStr, 10);
}
for (var el in subGroupElements) {
if (subGroupElements.hasOwnProperty(el)) {
currentGroupElements[el] = (currentGroupElements[el] || 0) + subGroupElements[el] * subMultiplier;
}
}
} else if (char === ')') {
i++; // Move past ')'
return currentGroupElements; // End of current group
} else if (/[A-Z]/.test(char)) {
var elementSymbol = char;
i++;
if (i < len && /[a-z]/.test(formulaString[i])) {
elementSymbol += formulaString[i];
i++;
}
if (atomicMasses[elementSymbol] === undefined) {
throw new Error("Unknown element symbol: " + elementSymbol);
}
var count = 1;
if (i < len && /\d/.test(formulaString[i])) {
var numStr = '';
while (i < len && /\d/.test(formulaString[i])) {
numStr += formulaString[i];
i++;
}
count = parseInt(numStr, 10);
}
currentGroupElements[elementSymbol] = (currentGroupElements[elementSymbol] || 0) + count;
} else if (/\s/.test(char)) { // Allow spaces in formula
i++;
} else {
throw new Error("Invalid character in formula: '" + char + "' at position " + (i + 1));
}
}
return currentGroupElements; // End of formula string
}
var parsed = parseGroup();
for (var el in parsed) {
if (parsed.hasOwnProperty(el)) {
elements[el] = (elements[el] || 0) + parsed[el];
}
}
return elements;
};
var calculateMolarMass = function() {
var formula = document.getElementById("chemicalFormula").value.trim();
var resultDiv = document.getElementById("molarMassResult");
resultDiv.innerHTML = ""; // Clear previous results
if (!formula) {
resultDiv.innerHTML = "Please enter a chemical formula.";
return;
}
var totalMolarMass = 0;
try {
var elementsCount = parseFormulaRecursive(formula); // Call the parser
for (var elementSymbol in elementsCount) {
if (elementsCount.hasOwnProperty(elementSymbol)) {
// atomicMasses check is already done within parseFormulaRecursive
totalMolarMass += atomicMasses[elementSymbol] * elementsCount[elementSymbol];
}
}
resultDiv.innerHTML = "The molar mass of " + formula + " is: " + totalMolarMass.toFixed(3) + " g/mol";
} catch (e) {
resultDiv.innerHTML = "Error: " + e.message + "";
}
};
Understanding Molar Mass
Molar mass is a fundamental concept in chemistry, representing the mass of one mole of a chemical compound or element. It is expressed in grams per mole (g/mol). Understanding molar mass is crucial for various chemical calculations, including stoichiometry, solution preparation, and determining reaction yields.
What is a Mole?
A mole is a unit of measurement used in chemistry to express amounts of a chemical substance. It is defined as exactly 6.02214076 × 1023 (Avogadro's number) elementary entities (atoms, molecules, ions, or other particles). Just as a "dozen" means 12, a "mole" means Avogadro's number of particles.
How to Calculate Molar Mass Manually
To calculate the molar mass of a compound, you sum the atomic masses of all the atoms in its chemical formula. Each atomic mass is multiplied by the number of times that atom appears in the formula. The atomic masses of individual elements can be found on the periodic table.
Here's a step-by-step guide:
Identify the elements: List all the elements present in the chemical formula.
Find atomic masses: Look up the atomic mass for each element from a periodic table.
Count atoms: Determine the number of atoms of each element in the formula (the subscripts). If there's no subscript, it means one atom. For elements within parentheses, multiply their subscript by the subscript outside the parentheses.
Multiply and sum: Multiply the atomic mass of each element by its count in the formula, then add all these values together.
Examples:
Water (H2O):
Hydrogen (H): 1.008 g/mol × 2 atoms = 2.016 g/mol
Oxygen (O): 15.999 g/mol × 1 atom = 15.999 g/mol
Total Molar Mass = 2.016 + 15.999 = 18.015 g/mol
Sodium Chloride (NaCl):
Sodium (Na): 22.990 g/mol × 1 atom = 22.990 g/mol
Chlorine (Cl): 35.453 g/mol × 1 atom = 35.453 g/mol
Total Molar Mass = 28.014 + 8.064 + 32.065 + 63.996 = 132.139 g/mol
Using the Molar Mass Calculator
Our Molar Mass Calculator simplifies this process. Simply enter the chemical formula into the input field, and the calculator will instantly provide the molar mass. It handles complex formulas, including those with parentheses, making your chemical calculations quick and accurate.
Note: The calculator uses standard atomic weights. Ensure your formula is correctly typed for accurate results.