Home >Web Front-end >JS Tutorial >The difference and usage of undefined and null that represent empty space in JavaScript

The difference and usage of undefined and null that represent empty space in JavaScript

黄舟
黄舟Original
2017-02-28 14:50:151923browse

When I first started learning JavaScript

I felt that undefined was quite new
Because other languages ​​​​don’t have
and not only undefined, but also a null that means empty
I believe many students can’t tell the difference between undefined and null.
Today I will summarize the difference between undefined and null, which means empty in JavaScript.

Similar

undefined and null are both JavaScript The basic types in
and they have only one type value each, which are undefined and null, which have the same names as their types
Normally, there is really no difference between these two values ​​

var foo = undefined;
var foo = null;

And the standard also has such a provision

console.log(null == undefined) // true

(but null !== undefined)

is converted into a Boolean value and is false

if(!undefined){
    console.log(1);//1}if(!null){
    console.log(1);//1}

Difference

But in fact, there is a difference
Let’s take a look at these lines of code first

console.log(undefined in window);// trueconsole.log(null in window);// false

This is the difference
null is the keyword of JavaScript
and undefined is window A special property on (the same as NaN and Infinity)
Although ES5undefined becomes a read-only property, there are still some unreasonable things, such as this

function fn(){
    var undefined = 1;// 注意:前方高能
    console.log(undefined);// 1}
fn();

Look, because undefined is not the key words, so we can declare undefined in local variables (simply speechless)
Even if you are in strict mode

function fn(){    "use strict";    var undefined = 1;
    console.log(undefined);// 1}
fn();

We must not write such outrageous code

In progress There are also differences in mathematical operations

console.log(undefined + 1); //NaNconsole.log(null + 1); //1

Any operation performed on undefined will be converted to NaN
and null is converted to 0

History

JavaScript has two values ​​that represent null It makes sense
The father of js, Brendan Eich, originally designed it like Java, with only null
But he considered that js has two basic values ​​and reference values
Using an object to represent empty monsters typeof null === 'object', and null is converted into the number 0, resulting in many bugs that are difficult to find
So he designed undefined

usage

According to the design of the father of js
undefined means a missing value
null means an empty object
But we don’t use it like this now

It can be understood this way
undefined means the variable is not initialized , no value
null means the variable has been initialized, and the value is "null value"

undefined is used by js, and null is for us
I don’t know if you can understand this sentence
For example, in these cases

  • var a; declare a variable, the engine will assign it a default value of undefined

  • ## during pre-compilation
  • #function a(){} Declare a function, if there is no return value, it will return undefined by default

  • var obj = {};obj.a; Access non-existent object properties, the default value is undefined

  • var a = function(a){};a(); No actual parameters are specified, The default parameter value is undefined

  • ......

This is the default undefined given to us by the system, which is the

used by js and we To use null
For example

  • var obj = Object.create(null); Create an empty object

  • demo.onclick = null; Unbind event

  • ......

As long as we want to use If it is empty, it is definitely correct to use null. In some cases, it can also facilitate the browser's garbage collection

Summary

Finally, let me summarize for everyone

  • undefined is a special attribute on window, it is an identifier, and can be assigned a value; null is a special keyword, not an identifier, and cannot be assigned a value

  • undefined == null –> true | undefined = == null –> false

  • undefined is converted into number and is NaN, null is converted into number and is 0

  • undefined and null is converted into boolean All are false

  • undefined means no value, null means empty value

  • We should use null to mean empty instead of undefined

  • Never redefine undefined

The above is the difference and usage of undefined and null that represent empty space in JavaScript. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn