Raise your hand if you have not yet encountered the unforgivable uncaught TypeError ‘ cannot read property of undefined ’ or ‘cannot read property of null’ printed on your browser console at least once when you were coding?
Have you ever asked why you are seeing them or what is the difference between null or undefined? I have come across a few of them in my experience as a software engineer. However, understanding each of these data types’ peculiarities has helped me to debug and to avoid some of these problems as well. I started to build more defensive programs once I understood their definition, their similarities, their differences and more importantly when to expect them in my program.
According to MDN documentation, a TypeError is thrown during the program execution when an operation cannot be performed normally because a value is not of the expected type. In this case, the operation could not handle values of type undefined and null. This blog attempts to define what each of these values is and highlights some of their most important differences and similarities.