Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the differences between null and undefined in JavaScript

Shulou Source: shulou.com Published: 2022-06-01 03:46:06 09月12日 Update

Editor to share with you what are the differences between null and undefined in JavaScript, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The difference between null and undefined?

First of all, Undefined and Null are both basic data types, and each of these basic data types has only one value, undefined and null.

The meaning of undefined is undefined, and the meaning of null is empty object (it is not a real object, see the note below! ). Undefined,null is returned when a variable is declared but not yet defined. It is mainly used to assign values to variables that may return an object as initialization.

In fact, null is not an object, although typeof null will output object, but this is only a long-standing Bug of JS. In the original version of JS, a 32-bit system was used to store the type information of variables in a low order for performance reasons. 000 represents an object, but null represents all zeros, so it is mistakenly judged as object. Although the internal type determination code has changed, the Bug has been handed down.

Undefined is not a reserved word in js, which means that we can use undefined as a variable name, which is very dangerous and affects our judgment of undefined values. But there are some ways to get a safe undefined value, such as void 0.

When we use typeof to judge two types, Null typing returns "object", which is a legacy of history. We return true when we compare two types of values using a double equal sign and false when we use three equal signs.

These are all the contents of this article entitled "what are the differences between null and undefined in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Type object variable representative article content meaning data time long dangerous secure three not so two code low information practice history Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux NVidia Shulou Tech Info OPPO Reno MySQL