What is the difference between null and undefined in JavaScript?

前端 未结 30 3362
夕颜
夕颜 2020-11-21 23:06

I want to know what the difference is between null and undefined in JavaScript.

30条回答
  •  梦毁少年i
    2020-11-21 23:51

    Basically, Undefined is a global variable that javascript create at the run time whether null means that no value has assigned to the variable (actually null is itself an object).

    Let's take an example:

            var x;  //we declared a variable x, but no value has been assigned to it.
            document.write(x) //let's print the variable x
    

    Undefined that's what you will get as output.

    Now,

            x=5;
            y=null;
            z=x+y;
    

    and you will get 5 as output. That's the main difference between the Undefined and null

提交回复
热议问题