• How to Implement a Two-Proportion Z-Test in Vanilla JavaScript (Zero Dependencies)
Build a lightweight A/B test statistical significance and confidence calculator in pure JS using the Abramowitz and Stegun normal CDF approximation.
When evaluating conversion rates between two variants (Control A vs. Variant B), comparing raw percentages isn't enough—you need to know whether the difference is statistically significant or simply random variance.
In this tutorial, we'll implement a two-proportion Z-test and normal cumulative distribution function (CDF) in pure, zero-dependency JavaScript.
1. The Statistical Formula
Given:
nA,cA= visitors and conversions for Variant AnB,cB= visitors and conversions for Variant B
First, calculate the individual conversion rates (pA, pB) and the pooled conversion rate (p):
pA = cA / nA
pB = cB / nB
p = (cA + cB) / (nA + nB)
Next, compute the standard error (SE) and the Z-score:
SE = sqrt( p * (1 - p) * (1/nA + 1/nB) )
Z = (pB - pA) / SE
2. Vanilla JavaScript Implementation
Because JavaScript's built-in Math object doesn't include an error function (erf), we can use the standard Abramowitz and Stegun approximation to convert our Z-score into a two-tailed confidence percentage:
function normalCDF(z) {
const t = 1 / (1 + 0.2316419 * Math.abs(z));
const d = 0.3989423 * Math.exp((-z * z) / 2);
const prob =
d *
t *
(0.3193815 +
t * (-0.3565638 + t * (1.781478 + t * (-1.821256 + t * 1.330274))));
return z > 0 ? 1 - prob : prob;
}
function calculateABSignificance(visitorsA, conversionsA, visitorsB, conversionsB) {
if (visitorsA <= 0 || visitorsB <= 0) return null;
const pA = conversionsA / visitorsA;
const pB = conversionsB / visitorsB;
const pooledP = (conversionsA + conversionsB) / (visitorsA + visitorsB);
const standardError = Math.sqrt(
pooledP * (1 - pooledP) * (1 / visitorsA + 1 / visitorsB)
);
if (standardError === 0) return { zScore: 0, confidence: 0, isSignificant: false };
const zScore = (pB - pA) / standardError;
const twoTailedPValue = 2 * (1 - normalCDF(Math.abs(zScore)));
const confidence = (1 - twoTailedPValue) * 100;
return {
conversionRateA: +(pA * 100).toFixed(2),
conversionRateB: +(pB * 100).toFixed(2),
zScore: +zScore.toFixed(3),
confidence: +confidence.toFixed(2),
isSignificant: confidence >= 95
};
}
// Example usage:
console.log(calculateABSignificance(5000, 210, 5000, 265));
// Output: { conversionRateA: 4.2, conversionRateB: 5.3, zScore: 2.587, confidence: 99.03, isSignificant: true }
3. Key Edge Cases to Guard Against
- Zero-division: Always check that
visitorsA > 0andvisitorsB > 0, and thatstandardError !== 0(which occurs if both variants have 0% or 100% conversion). - Sample size: Even if
confidence >= 95%, stopping a test with fewer than ~100 conversions per variant often produces false positives.
Live Implementation
I originally wrote this helper while building the client-side A/B split-test engine on FinCalcHub. Feel free to adapt the snippet above for your own analytics dashboards or internal tools!
About the author
- Founder of FinCalcHub.xyz — 15+ free, zero-backend calculators for marketing, SaaS unit economics, freelancing, and investing.
Comments
Join the discussionNo comments yet. Be the first to comment.

