Computer science: JavaScript: null value, undefined type and value

The tutor comments on JavaScript null and undefined.

Null, it seems, is often mentioned in computer science. Perhaps it’s used a little loosely: it might refer to the value read when there’s nothing there, or the value of a variable that doesn’t exist.

In JavaScript, in my experience:

  • A variable that doesn’t exist at all is of type undefined. Trying to print its value will prevent the script from running.
  • A variable that is declared but not given a value (eg var var1;) is also type undefined. However, the program will print its value as undefined.
  • A variable declared with a value null (eg var var1 = null;) is of type object. Its value will print out null.

    For a possible reason why a variable set as null is of type object, see my post here about Java and null. JavaScript isn’t the same as Java, but the two languages may relate to null with some similarity.

    Source:

    w3schools.com

    Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Tagged with: ,

Leave a Reply