Search code examples
javascriptreferenceerror

When does javascript throw reference error?


I have this very simple question, but I can not find any complete answer for this question.

JavaScript's null and undefined is the values of variables that have not been initialised and the value you get when you query the value of an object property or an array element that does not exist.

I first thought we get a referenceerror when we attempt to access a non-existent property of an object or an array element that does not exist, but as it turns out it isn't the case.

But on which occasions actually does it throw a referenceerror?

I appreciate if someone can tell all the situations when it can throw a referenceerror


Solution

  • You get a reference error when you:

    • Attempt to read a variable that has not been declared
    • Attempt to write to a variable that has not been declared when working in Strict Mode (in legacy mode this will create an implicit global instead)

    JavaScript's null and undefined is the values of variables that have not been initialised

    No. A variable that has been declared but not assigned a value or a property that has not been assigned a value will have undefined as a default value. undefined can also be explicitly assigned. null can only be explicitly assigned.