Difference between undefined and null



Can someone explain to me what the difference is between undefined and null?


Answer ( 1 )


    NULL is a special value which represents nothing or empty. You can initially set a variable to NULL. For example:

    let fname = null;

    Maybe we don’t know the name yet. Later we can change it.

    fname = "John";

    The variable below is undefined:

    let age;

    it is undefined because a value hasn’t been assigned to it yet.

    Null is an assignment value, it is assigned to a variable, as seen above.

    We have an article about Javascript data types, check it out.

Leave an answer