JS in six data types (a)--undefined (turn)

Source: Internet
Author: User

There are 6 types of data in JS: Undefined, Null, Boolean, number, string, and object. Any mechanism for creating custom types is not supported in JS, and all values will eventually be the 6 data types. The dynamic nature of its data types is sufficient to represent all data. JS provides the typeof operator to detect the data type of a variable.

(typeof returns a string of six possible types: "Number", "string", "Boolean", "Object", "function", "undefined").

Let's take a look at undefined this data type, undefined type has only one special value--undefined. undefined means "missing value", that is, there should be a value here, but there is no definition. Typical uses are:

(1) The variable is declared, but when it is not assigned, it is equal to undefined.

(2) when calling a function, the argument that should be supplied is not provided, which equals undefined.

(3) The object does not have an assigned property, and the value of this property is undefined.

(4) When the function does not return a value, undefined is returned by default.

For example:

var color;

Alert (message = = undefined);//true

This example only declares the variable color, but does not initialize it. Comparing this variable with the undefined literal, the result shows that they are equal. This example is equivalent to the following example:

var color = undefined;

Alert (color = = undefined);//true

However, a variable that contains a undefined value is not the same as a variable that has not been defined:

var color;//The undefined value is obtained by default after a variable declaration

Non-declared variable

var age

alert (color); "Undefined"

alert (age); Error generated

However, executing the typeof operator on an uninitialized variable returns the undefined value, and the TypeOf operator that executes the undeclared variable also returns the undefined value.

Undefined value is obtained by default after var color;//variable declaration

Non-declared variable

var age

Alert (typeof color); "Undefined"

Alert (typeof age); "Undefined"

The results show that the typeof operator, which executes the uninitialized and undeclared variables, returns the undefined value, and the result is logically justified. Because although these two variables are fundamentally different from the technical point of view, it is virtually impossible to perform real operations on any of these variables.

Welcome to the public number: Wcs290130--javascript that thing.

JS in six data types (a)--undefined (turn)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.