Get the App
SLTechnology News&Howtos  ›  Development  › 

How does javascript determine whether the value is undefined?

Shulou Source: shulou.com Published: 2022-06-01 18:54:55 09月20日 Update

This article mainly introduces the relevant knowledge of "how to judge whether the value of javascript is undefined". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope that this article "how to judge whether the value of javascript is undefined" can help you solve the problem.

In javascript, you can use the typeof operator and the "= =" operator to determine whether the specified value is undefined, and the syntax "if (typeof (specified value) = =" undefined ") {/ / value is undefined;}".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Undefined Typ

Undefined is a special data type with only one value, indicating that it is undefined. When we declare a variable without assigning a value to it, the default value for that variable is Undefined. For example:

Var num;console.log (num); / / output undefined

So how do you tell if the value is undefined?

In javascript, you can use the typeof operator to determine whether the value is undefined.

When you use the typeof operator to look at unassigned variable types, you will find that they are also of type undefined. If you look at the type of an undeclared variable using the typeof operator, you will find that the undeclared variable is also undefined. The sample code is as follows:

Var message;console.log (typeof message); / / output undefinedconsole.log (typeof name); / / output undefined

The method of judgment:

Var tmp = undefined;if (typeof (tmp) = = "undefined") {alert ("undefined");}

Description: typeof operator

Typeof is a unary operator that returns a string of Operand types.

The data type of NaN is number

The data type of the Array is object

The data type of date (Date) is object

The data type of null is object

The data type of the undefined variable is undefined

Typeof [1, 2, 3, 5]; / return objecttypeof new Date (); / return objecttypeof NaN; / / return numbertypeof function () {} / / return functiontypeof myCar; / / return undefined (if myCar does not declare)

The difference between undefined and null:

Typeof null; / / return objecttypeof undefined; / / return undefinednull = undefined; / / falsenull = = undefined; / / true on "how to determine whether the value of javascript is undefined" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Type variable data operator knowledge output method industry operator operation different practical special code content only character string utility actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft Apple vpn OPPO Reno