Uncaught TypeError: Cannot read property 'top' of undefined

I’m getting an uncaught type error at line 5 and can’t seem to figure out why. Any help would be appreciated.

(function($) {
  "use strict";

  var $navbar = $(".nav"),
    y_pos = $navbar.offset().top,
    height = $navbar.height();

  //scroll top 0 sticky
  $(document).scroll(function() {
    var scrollTop = $(this).scrollTop();
    if (scrollTop > 0) {
    } else {
})(jQuery, undefined);

What is line 5 ???

My guess would be that the element with the class nav doesn’t exist in your HTML

This is line five:

y_pos = $navbar.offset().top,

That’s probably because you linked app.js before body tag and therefore it runs before DOM is loaded and element with the class nav is created.
Move <script src="app.js"></script> before </body>

‘Undefined’ is the property of the global object. This error occurs in Chrome Browser when you read a property or call a method on an undefined object . Uncaught TypeError: Cannot read property of undefined error is probably easiest to understand from the perspective of undefined, since undefined is not considered an object type at all (but its own undefined type instead), and properties can only belong to objects within JavaScript. There are a few variations of this error depending on the property you are trying to access. Sometimes instead of undefined it will say null. If you get undefined error, you need to make sure that which ever variables throws undefined error, is assigned a value to it.