Home  >  Article  >  Web Front-end  >  What is the difference between undefined and null in javascript

What is the difference between undefined and null in javascript

清浅
清浅Original
2019-03-05 14:56:548740browse

The difference between undefined and null in javascript is: different types, the former returns an undefined value and the latter is an object; the method of converting the original type is different, the former does not support conversion and the latter value is 0

Most of them Anyone using JavaScript misunderstands the relationship between null and undefined, and unclear relationships between these two entities can lead to serious problems. Today I will share with you how to distinguish between null and undefined, which has a certain reference effect. I hope it will be helpful to everyone

What is the difference between undefined and null in javascript

[Recommended course: JavaScript Tutorial

Null value in JavaScript

null is a keyword in JavaScript, which means there is no value or no value exists. For example, if we want to remove a variable from a specified value, we can simply specify its value as null. Beyond that, like any other object, it is never implicitly assigned to a variable by JavaScript. Example:

var demo= null;
console.log(demo)

What is the difference between undefined and null in javascript

undefined in JavaScript

undefined is a global variable created by JavaScript at runtime, in one of the following situations Assign this global variable to an object

1, an object declared but not initialized or defined

2, an array index or object property that does not exist

3, not Function parameters provided

4. Return value of a function that is required but does not return a value

var demo;
console.log(demo)

What is the difference between undefined and null in javascript

The difference between undefined and null

1. Null and undefined have different types

console.log(typeof(undefined)); //undefined
console.log(typeof(null));    //object

null is an object with a valid non-existent value, and it is immutable, while the undefined object The type is itself undefined

Furthermore any arithmetic operation with a null value will produce an integer value, and any arithmetic operation with undefined will cause the variable value to become NaN

2. Different ways of converting to primitive types

The main difference between null and undefined is the way they are converted to primitive types. When performing an arithmetic conversion on null, the determined value is 0. This conversion can be verified using the following code snippet.

var v1= 5+ null;
console.log(v1)

The output result is 5

But undefined does not perform any such conversion, if undefined is added to the number the result will be NaN

var v2= 5+ undefined;
console.log(v2)

The output result Summary for NaN

: The above is the entire content of this article, I hope it will be helpful to everyone.

The above is the detailed content of What is the difference between undefined and null in javascript. For more information, please follow other related articles on the PHP Chinese website!

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