Global variable very basic

Global variable very basic

Hello everyone, 

I have a very basic question with global variable and Jquery.

Here i is a counter. I want to know its last value and keep it out of the function
  1. <div class="aremplir">
  2. </div>
  3. <button type="button" id="ajouter">Ajouter</button>

  4. <script type="text/javascript">
  5.     jQuery(function($){
  6.  var i = 0;
  7.   $('#ajouter').click(function(e){
  8.       e.preventDefault();
  9.       var content = "<div id='input_name"+i+"'><input type='text' /></div>";
  10.       i++;
  11.       $('.aremplir').append(content);
  12.       mon_dernier_i = i;
  13.   });

  14.  if(typeof mon_dernier_i !='undefined'){
  15.   console.log(mon_dernier_i);
  16. }
  17. });

  18. </script> 
it seems that mon_dernier_i is not defined : nothing happens. I thought that not declaring 'var' in the function would make 'mon_dernier_i' global but obviously I am wrong...

Can anyone explain it to me ? 

Thank you very much