javascript-Data types

Source: Internet
Author: User

Javasscript has 5 simple data types (also becoming basic data types):

Undefined, Null, Boolean, number, String

1 Types of complex data:

Object

The data type of all values in JavaScript is one of the above 6. Then we need a way to detect what type of data the given variable is, and the typeof operator can help us solve the problem.

var msg= "blog Park"; alert (typeof msg);  "String" alert (typeof);  "Number"

Use the typeof operator to return one of the following strings:

"Undefined"--the value is undefined

"Boolean"--Boolean value

"String"--string

"Number"--value

"Object"--object or null

"Function"--functions

See here we have some doubts about "object"--objects or null , why typeof Null will return object, here we need to remember is that NULL is a special type, he represents a reference to an empty object, so it returns an object.

Gorgeous split-line ----------------------------------------------------------------------------------------------------------- -----------------------------------------------------

1.Undefined type

When we declare a variable without initializing it, the value of this variable is undefined:

var msg;alert (msg==undefined); True

The example declares the variable msg and does not initialize it, so it pops up undefined.

So we can also use undefined to initialize variables when declaring variables: Var msg=undefined,

However, uninitialized values are undefined by default, and this is not a necessary notation.

var msg; Declare variable, default get undefined//variable name not declared//var name alert (msg); "Undefined" alert (name); Error generated

The above code name variable is not declared and an error is generated when passed to the alert function.

Then we use typeof to check our variables:

var msg; Declare variable, default get undefined//variable name no declaration//var name alert (typeof msg); "Undefined" alert (typeof name); "Undefined"

The result is that uninitialized and undeclared variable execution typeof all return undefined, which is logically justified. Because although these two kinds of variables from the technical point of view have the essential difference.

Even if the uninitialized variable is automatically assigned the undefined value, the habit of explicitly initializing the variable (that is, assigning a variable to the variables while declaring the variable) is made. If you can do this, then when the typeof operator returns undefined

, we know that the variables being detected are or are not declared, not yet initialized.

2.Null type

As I said before, NULL is a special type that represents a reference to an empty object.

But what is the relationship between undefined and null? Look at the following code:

alert (null==undefined); True

In fact, undefined values derive from null values, so ECMA-262 prescribes their equality tests to return Teue.

In spite of this relationship, their use is completely different. As stated earlier, declaring variables is not necessary to display the initialization as undefined, which is obtained by default. For NULL, this is not the case, which means declaring a variable to hold the object, as long as

If the variable is not saved, you should let the variable explicitly save the null value. This can both represent null as an attribute of null object pointers, and can distinguish between null and undefined.

3.boolean type

A Boolean type has only two values: True and False. These two values are not the same as numeric values, so true is not necessarily equal to 1,false or not necessarily equal to 0.

The literal value of Boolean literals True and false is case-sensitive. Like true and False are not Boolean values, but identifiers.

Although the Boolean type has only two values, other types of values have values that are equivalent to these two Boolean values. To convert a value to its corresponding Boolean value, you need to call the Boolean () function.

var msg= "blog Park"; Alert (Boolean (msg)); True

In the preceding code, the string msg is converted to a Boolean value. You can call the Boolean () function on a value of any data type, and always return a Boolean value. Whether this value is true or false depends on the data type and the specific value of the numeric value being converted.

See the following data types and the corresponding conversion rules:

A value that converts the value of the data type to true to False

                      boolean                              ,         &NB Sp          true                                                  ,         &N Bsp                false        &NBSP

                        &NBSP ; string                     &NB Sp                                 non-empty string   ;                          ,         &NB Sp                             "empty string  

                        &NBSP ; number                     &NB Sp                             Non 0 value     &NBSP ;                          ,         &NB Sp                      &NBSP     0 and NaN,

                        &NBSP ; objeact                     &N Bsp                             Any object     &NBSP ;                          ,         &NB Sp                            null           

undefined------Not applicable Undefined

These conversion rules can help us understand the control flow statement if automatic execution of the corresponding Boolean transformation:

var msg= "Blog Park", if (msg) {    alert ("variable msg is converted to true");}

When you run the above code, a warning box is displayed. The string msg is automatically converted to the corresponding Boolean value. By understanding this principle, we will be able to use variables in the control statement if.

4.number type

3.1 integers, the most commonly used is the decimal integer. Of course, there are eight hexadecimal, and there is no explanation here.

It is important to note that when doing arithmetic calculations, all numeric values in eight and hexadecimal are eventually converted to decimal values.

3.2 Floating-point value, that is, the number must contain a decimal point, after the decimal place must have a digit.

var num1=1.1;var num2=1.2;var num3=.3;  Valid, but not recommended

Because the memory space required to hold a floating-point value is twice times the value of the saved integer, ECMAScript automatically converts the corresponding floating-point value to an integer value. If the decimal point is not followed by a value (2.) or followed by a 0 (2.0),

This value is saved as an integer value:

var num1=2.; alert (NUM1);  2, there is no numerical resolution after the decimal point to 2var Num2=2.0;alert (num2);  2, which is itself an integer resolution of 2

For those large or small values, a floating-point value can be represented by the E notation (scientific notation).

Format of the scientific notation: integers/floats + exponents in the power of e/e + 10

3.125 e 7 = =

var Num1=3.125e7;  31250000

3.3 Range of values, due to memory limitations, and cannot save all values.

The minimum value that can be represented: Number.min_value, this value is 5e-324.

Maximum value that can be represented: Number.MAX_VALUE, this value is 1.7976931348623157e+308.

If your calculated value is not within the range of the minimum and maximum JavaScript values, then your value will be automatically converted to a special infinity value.

If the value is negative, it is converted to-infinity (negative infinity), and if it is positive, it turns into Infinity (positive infinity).

3.4 NaN: Non-numeric. There are two features:

One, any operation that involves Nan will return Nan (for example, NAN/5).

Two, Nan is not equal to any value, including itself.

alert (Nan==nan); False

For these two characteristics of Nan, JavaScript defines the isNaN () function. This function takes a parameter that can be any type, and the function will help us determine if the parameter is "not a value".

IsNaN () After a value is received, attempts to convert the value to a numeric value, some values that are not numeric are converted directly to numeric values (string "10" or Boolean), and any that cannot be converted will return true

Alert (IsNaN (NaN)); Truealert (IsNaN (10)); False 10 is a numeric alert (IsNaN ("10")); False "10" can be converted to a value of 10alert (IsNaN ("blog Park")); True cannot be converted to numeric alert (IsNaN (false)); False can be converted to 0

5.String type

A string is a basic type of data for JavaScript.

The length property of the string object declares the number of characters in the string.

The string class defines a number of methods for manipulating strings, such as extracting characters or substrings from a string, or retrieving characters or substrings.

6.object type


A set of data and features that an object can create by using the new operator.

var obj=new Object ();

You can customize the properties or methods of an object:

var obj=new Object (); Obj.name= "Zhang San"; Obj.say=function () {    alert (this.name)}

You can also write this:

var obj={    name: "Zhang San",    say:function () {        alert (this.name)    }}

The result is the same.

object is the basis of all objects in JavaScript, so all objects have properties and methods of object.

javascript-Data types

Related Article

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.