Calculating key real estate investment metrics—such as amortization, cash flow, NOI, cap rate, and cash-on-cash return—is essential for evaluating rental property performance. This article walks through implementing these calculations in JavaScript using verified formulas and test cases from the rental-roi-formulas repository.
Core Formulas and Implementation
The foundation of the calculation begins with the monthly mortgage payment, calculated using the standard amortization formula:
M = P * r(1+r)^n / ((1+r)^n - 1)
Where:
-
M= monthly payment -
P= loan principal -
r= monthly interest rate -
n= number of payments (term in months)
This is implemented in JavaScript as monthlyMortgagePayment(principal, annualRatePct, termYears).
The computeROI function processes all inputs and computes the following metrics:
-
Down Payment:
price × (downPct / 100) -
Loan Amount:
price - downPayment - Mortgage Payment: Calculated using the amortization formula
- Operating Expenses (OpEx): Includes fixed costs (tax, insurance, HOA, other) plus variable costs based on rent (maintenance, vacancy, management)
-
Monthly Cash Flow:
rent - (mortgage + monthlyOpex) -
Annual Cash Flow:
monthlyCashFlow × 12 -
Cash Invested:
downPayment + closingCosts + upfrontRepairs -
Cash-on-Cash Return:
(annualCashFlow / cashInvested) × 100 -
Net Operating Income (NOI):
annualRent - annualOpex(mortgage excluded) -
Cap Rate:
(NOI / price) × 100
Example Calculation
Using the demo input:
- Price: $250,000
- Down payment: 20% → $50,000
- Interest rate: 6.5%
- Term: 30 years
- Closing costs: $5,000
- Upfront repairs: $0
- Rent: $2,000/month
- Property tax: $250/month
- Insurance: $100/month
- Maintenance: 5% of rent → $100
- Vacancy: 5% of rent → $100
- Management: 0%
The computed values are:
- Mortgage: $1,264.14/month
- Monthly OpEx: $550
- Monthly Cash Flow: $185.86
- Annual Cash Flow: $2,230.37
- Cash Invested: $55,000
- Cash-on-Cash: 4.06%
- NOI:
17,400 (rent of24,000 a year minus $6,600 of operating expenses, mortgage excluded) - Cap Rate: 6.96%
Why Cap Rate Ignores Financing
The cap rate (capitalization rate) is a measure of return based solely on property performance, not financing. It is calculated as:
cap rate = NOI / purchase price
Because NOI excludes mortgage payments (which are financing costs), the cap rate reflects the property’s ability to generate income regardless of how it was purchased. This allows investors to compare properties on a level playing field—whether financed with a loan or paid in cash.
For example, in the demo:
- NOI = $17,400
- Price = $250,000
- Cap Rate =
17,400 /250,000 = 6.96%
Set the down payment to 100% and the cap rate stays at 6.96%, while monthly cash flow rises to $1,450 (no mortgage) and cash-on-cash becomes about 6.82%. The cap rate does not move because it is computed before financing; the other two numbers do. The inputs above are made up for illustration and do not describe a real property.
Verification and Use
npm test checks these cases:
-
200,000 loan at 6% over 30 years →1,199.10/month - $120,000 loan at 0% interest over 10 years → $1,000/month
- All-cash purchase:
200,000 price,1,500 rent,300 in fixed expenses → NOI =14,400, cap rate = 7.2%, cash-on-cash = 7.2% - 5% vacancy on
1,500 rent adds75 to monthly operating expenses
Educational Use Only
This code is for educational purposes and does not constitute investment advice. Actual real estate decisions require due diligence, market analysis, and professional consultation.
For full source code and interactive tools, visit:
Top comments (0)