Home > other >  Reference : TypeError: Cannot read property [property name here] from undefined
Reference : TypeError: Cannot read property [property name here] from undefined

Time:10-21

Derived from Syntax error: Syntax error. line: 4 file: Code

  • Checking for type

    Especially true for the special case, typeof operator combined with an if statement and a comparison operator will either allow a function to run outside of its designated context (i.e. for debugging purposes) or introduce branching logic depending on whether the object is present or not.

    One can control how strict the check should be:

    • lax ("not undefined"): if(typeof myVar !== "undefined") { //do something; }
    • strict ("proper objects only"): if(typeof myVar === "object" && myVar) { //do stuff }
  • Key Terms

    Object
    It's one of the JavaScript data types.
    undefined
    It's one of the JavaScript primitive data types.

    To learn about the very basics of JavaScript data types and objects see What are JavaScript Data Types? and What exactly is an object? [JavaScript].


    Original revision extracted from here (Credit to @OlegValter)

    • Related