 <p>As a function:</p> <pre><code>function printYearsBefore(date,num) { var yr = parseInt(date.getFullYear()), i =0; while(++i &lt;= num){ document.write((yr-i)); } } printYearsBefore(new Date(),18); </code></pre>