Download Source Code: TaxCalculator.zip - 14.88KB
Canadian Income Tax Calculator in JavaScript
This implementation can be run locally and does not require deployment on a web server. When you click the Calculate button, the result area is filled with a table with total taxes for each province (including the federal), depending on your Taxable Income value. You can try it directly here or as a generic online tool:
Taxable Income:
Tax-Calculator.html, that you can call separately from our project, includes a JavaScript file Tax-Calculator.js. This HTML client-side based solution has a main IncomeTax function in JavaScript and an array with all tax-related parameters.
The tax-related algorithm is pretty simple and was basically implemented within the two embedded for loops below:
function IncomeTax()
{
var result = document.getElementById("result");
if (typeof(result)=="undefined")
return;
result.innerHTML = "Taxable Income: $1 .. $10,000,000";
var income = document.getElementById("income");
if (typeof(income)=="undefined")
return;
income.value = ToInteger(income.value);
if (isNaN(income.value))
return;
var salary = parseInt(income.value);
if (salary<=0 || salary>10000000)
return;
income.value = ToMoney(salary);
var taxF = 0;
var s = "<table cellspacing=\"5\" "
+ "style=\"border:solid 1px gainsboro; font-size:80%;\">"
+ "<tr><th>Province</th>"
+ "<th>Prov Taxes</th><th>Fed Taxes</th>"
+ "<th>Net Income</th></tr>";
for (var i=0; i<taxes.length; )
{
if (taxF>0)
s += "<tr><td><i>" + taxes[i] + "</i></td>";
i++;
var tax = 0;
for (var left=salary; left>0; i+=2)
{
if (taxes[i+1]>0 && left>=taxes[i+1])
{
tax += taxes[i] * taxes[i+1];
left -= taxes[i+1];
}
else
{
tax += taxes[i] * left;
left = 0;
}
}
if (taxF==0)
taxF = tax;
else
s += "<td>" + ToMoney(tax)
+ " (" + ToPercent(tax/salary)
+ ")</td><td>" + ToMoney(taxF)
+ " (" + ToPercent(taxF/salary)
+ ")</td><td><b>" + ToMoney(salary-tax-taxF)
+ "</b> (" + ToPercent((salary-tax-taxF)/salary)
+ ")</td></tr>";
while (i<taxes.length && !isNaN(taxes[i]))
i += 2;
}
result.innerHTML = s + "</table>";
}