Home Web Front-end JS Tutorial Summary of the differences between null, NaN and undefined in Javascript_javascript skills

Summary of the differences between null, NaN and undefined in Javascript_javascript skills

May 16, 2016 pm 05:37 PM
null undefined

1. Type analysis :
The data types in js include undefined, boolean, number, string, object, etc. The first 4 types are primitive types, and the fifth type is reference type.
Code

Copy code The code is as follows:

var a1;
var a2 = true;
var a3 = 1;
var a4 = "Hello";
var a5 = new Object();
var a6 = null;
var a7 = NaN;
var a8 = undefined;
alert(typeof a); //Display "undefined"
alert(typeof a1); //Display "undefined"
alert(typeof a2); //Display "boolean" "
alert(typeof a3); //Display "number"
alert(typeof a4); //Display "string"
alert(typeof a5); //Display "object"
alert (typeof a6); //Display "object"
alert(typeof a7); //Display "number"
alert(typeof a8); //Display "undefined"

From the above code, we can see that undefined values ​​and unassigned values ​​are undefined, null is a special object, and NaN is a special number.
2. Comparison operation
Copy code The code is as follows:

var a1; //The value of a1 is undefined
var a2 = null;
var a3 = NaN;
alert(a1 == a2); //Display "true"
alert(a1 != a2); //Display "false"
alert(a1 == a3); //Display "false"
alert(a1 != a3); //Display "true"
alert( a2 == a3); //display "false"
alert(a2 != a3); //display "true"
alert(a3 == a3); //display "false"
alert (a3 != a3); //Display "true"

From the above code we can draw the conclusion: (1) undefined and null are equal; (2) NaN is not equal to any value , not equal to oneself.

JavaScript undefined attribute

Definition and usage The
undefined attribute is used to store the undefined value of JavaScript.

Syntax
undefined

Explanation
You cannot use for/in loop to enumerate undefined properties, nor can you use delete operation character to delete it.
undefined is not a constant and can be set to other values.
Undefined will also be returned when trying to read a non-existent object property.
Tips and Notes
< The value is equivalent to undefined The operator is used to test whether a value is undefined, because ="=" ="==">
< means None value, while>
Copy code
The code is as follows:

Output:
[Supplement] Null data type
In Jscript, the data type null has only one value: null. The keyword null cannot be used as the name of a function or variable.
A variable containing null contains "no value" or "no object". In other words, the variable does not hold a valid number, string, boolean, array, or object. You can clear the contents of a variable by assigning a null value to it.

Note that in Jscript, null is not equal to 0 (unlike in C and C). It should also be noted that the typeof operator in Jscript will report null values ​​as type Object, not type null. This potential confusion is for backward compatibility.
Undefined data type
Undefined value is returned in the following situations:
The object property does not exist,
The variable is declared but never assigned a value.

Note that you cannot test whether a variable exists by comparing it to undefined, although you can check whether its type is "undefined". In the following code example, assume that the programmer wants to test whether the variable

// This method does not work
if (x == undefined)
// Do something
// This method also does not work - must check
// String "undefined"
if (typeof(x) == undefined)
// Do some operations
// This method is valid
if (typeof(x) == "undefined ")
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to PHP Notice: Undefined property: Solution to PHP Notice: Undefined property: Jun 22, 2023 pm 02:48 PM

When writing code in PHP, we may encounter the error message "Notice: Undefinedproperty". This error means that we are accessing an undefined property, usually because the property has not been initialized in the code. So, how to solve this problem? Here are a few possible solutions: Initialize properties This is the simplest way to solve this problem. Explicitly initializing a property in code ensures that it is defined before use. For example: class

TypeError: Cannot read property '$XXX' of undefined in Vue, how to deal with it? TypeError: Cannot read property '$XXX' of undefined in Vue, how to deal with it? Nov 25, 2023 pm 12:14 PM

If you often encounter the error message "TypeError: Cannotreadproperty'$XXX'ofundefined" when developing with Vue.js, how should you deal with it? This article explains the causes of this error and how to fix it. The cause of the problem is that when using Vue.js, we often use this to call methods of Vue components, such as: exportdefault{data()

What is the difference between null and NULL in c language What is the difference between null and NULL in c language Sep 22, 2023 am 11:48 AM

The difference between null and NULL in C language is: null is a macro definition in C language, usually used to represent a null pointer, which can be used to initialize pointer variables, or to determine whether the pointer is null in a conditional statement; NULL is a macro definition in C language A predefined constant in , usually used to represent a null value, used to represent a null pointer, null pointer array or null structure pointer.

What do undefined and null mean? What do undefined and null mean? Nov 20, 2023 pm 02:39 PM

In JavaScript, both undefined and null represent the concept of "nothing": 1. undefined represents an uninitialized variable or a non-existent property. When a variable is declared but no value is assigned to it, the value of the variable is undefined , when accessing properties that do not exist in the object, the returned value is also undefined; 2. null represents an empty object reference. In some cases, the object reference can be set to null to release the memory it occupies.

what is undefined what is undefined Jul 31, 2023 pm 02:28 PM

undefined represents a state in which a value or variable does not exist or is undefined. It can be used as a default value to determine whether a variable has been assigned a value, and can also be used to set default parameter values. Although undefined may have different meanings and usages in different programming languages, understanding the concept of undefined can help us better understand and write programs.

How to solve 'undefined: path.Join' error in golang? How to solve 'undefined: path.Join' error in golang? Jun 24, 2023 pm 01:12 PM

In the Go language, the path package is one of the important tools for processing file paths. The path.Join() function can combine multiple paths into a complete path. However, sometimes you will encounter the error message "undefined: path.Join", what should you do? Here are several common solutions: Check the import statement First, you need to confirm that you have imported the path package correctly. In Go language, when importing a package, you can

What is the difference between null and undefined What is the difference between null and undefined Nov 08, 2023 pm 04:43 PM

The difference between null and undefined is: 1. Semantic meaning; 2. Usage scenarios; 3. Comparison with other values; 4. Relationship with global variables; 5. Relationship with function parameters; 6. Nullability check; 7. Performance considerations; 8. Performance in JSON serialization; 9. Relationship with types. Detailed introduction: 1. Semantic meaning, null usually means knowing that this variable will not have any valid object value, while undefined usually means that the variable has not been assigned a value, or the object does not have this attribute; 2. Usage scenarios, etc.

When to use null and undefined When to use null and undefined Nov 13, 2023 pm 02:11 PM

Both null and undefined indicate a lack of value or an undefined state. Depending on the usage scenario, there are some guiding principles for choosing to use null or undefined: 1. When you need to clearly indicate that a variable is empty or invalid, you can use null; 2. When a variable has been declared but not yet assigned a value, it will be set to undefined by default; 3. When you need to check whether a variable is empty or undefined, use the strict equality operator "===" to determine whether the variable is null or undefined. .

See all articles