Assigning before $(document).ready

Assigning before $(document).ready

Hi,

I saw this code:
  1. var $element = $('#element');
  2.  
  3. $(document).ready(function()
  4. {
  5.       console.log($element);
  6. });

If I understand this correctly above statement will work in most cases, but it's not 100% sure since DOM might not be loaded yet.

This would be the better way, right?
  1. var $element;
  2.  
  3. $(document).ready(function()
  4. {
  5.       $element = $('#element');

  6.       console.log($element);
  7. });
I just want confirmation that the first example might not always work.

Thanks in advance!