JavaScript data type learning notes

Source: Internet
Author: User

Data types are available in every programming language. js data types include: number type, string type, Boolean Type, indicates an object whose variable is not assigned a value or is empty. Let me introduce it to you.

Here we will only explain the simple data types in section 5, and the functions and objects will be explained in subsequent chapters.


Basic Data Types

Number numeric type
String type
Boolean Type
Undefined indicates that a variable is not assigned a value.
Null Object

Number Type

The Number type is used to represent integers and floating-point numbers. For example:
Var x = 100; the Number type also has a special value NaN (Not a Number ).

NaN is used to indicate that a numeric value is not returned for an operation, so that no error is thrown.

String type

String indicates a String. A string can be expressed by double quotation marks ("") and single quotation marks.

For example:

The Code is as follows: Copy code

Var name = "I'm Tom! ";
Var address = 'I com from China! ';

Boolean Type

Boolean is a Boolean type with only two values: true and false ).

The Boolean type is often used in flow control statements, for example:

The Code is as follows: Copy code
Var x = true;
If (x ){
Alert ("x = true ");
}

Run the code. A warning box is displayed, showing "x = true ".
Undefined type
The Undefined type has only one value, that is, undefined. When a variable is declared but not assigned a value, its value is undefined. For example:

The Code is as follows: Copy code

Var name;
Alert (name); // display undefined

Click here to call an unassigned variable

Null type
There is only one Null type value, that is, null. Null indicates an empty object.

If the defined variable is to be used to save the object in the future, it is better to initialize the variable to null instead of other values.
Dynamic type
JavaScript is a weak type language, which means that JavaScript has dynamic types, and the same variable can be assigned different types of values. This is not possible in strong languages such as C, C ++, and Java.

The following values are correct:

The Code is as follows: Copy code

Var demo = 1; // This is the Number type
Demo = true; // This Is A Boolean type
Demo = "Hello"; // This is String type

Var;
C = new function () {var a = 1; alert (typeof ());};
Alert (typeof ());

Result: number, undefined;

The Code is as follows: Copy code
Var a = 1;
C = new function () {alert (typeof (a); var a = 1 ;};
Alert (typeof ());

Result: undefined, number;

The Code is as follows: Copy code
Var;
C = new function () {alert (typeof (a); a = 1 ;};
Alert (typeof ());

Result: undefined, number;

The Code is as follows: Copy code

Var;

C = new function () {var a = 1; alert (typeof ());};

You can use this. a in c to access global variable.

================


Specify their values

1. typeof (NaN) number, typeof (Infinity) number, typeof (null) object, typeof (undefined) undefined
2. NaN = NaN false
3. NaN! = NaN true
4. NaN> = NaN false
5. null = undefined true
6. null> = undefined false
7. null <= undefined false
8. parseInt ("123abc") 123
9. "123abc"-0 NaN
10. Infinity> 10 true
11. Infinity> "abc" false
12. Infinity = NaN false
13. true = 1 true
14. new String ("abc") = "abc" true
15. new String ("abc") = "abc" false identical

Relational operators (<,>, <=,> =)

• Try to convert both expression1 and expression2 into numbers.
• If both expressions are strings, the strings are compared in lexicographically.
• If one of the expressions is NaN, false is returned.
• Negative zero equals to positive zero.
• Negative infinity is smaller than any number including itself.
• Positive infinity refers to any number including itself.
Equal operator (= ,! =)

• If the two expressions have different types, try to convert them into strings, numbers, or Boolean values.
• NaN is not equal to any value including itself.
• Negative zero equals to positive zero.
• Null is equal to null and undefined.
• The same string, number, object, Boolean value, or (when the types are different) can be forcibly converted to one of the above cases, are considered equal.
• Other comparisons are considered unequal.
Constant equal operator (= ,! =)

Except for no type conversion and the types must be the same, these operators have the same role as the equal operators.


Output results

1,

The Code is as follows: Copy code
Var a = "123abc ";
Alert (typeof (a ++); string, ++ operator is not executed in typeof
Alert (a); NaN

2. a is of the string type.

The Code is as follows: Copy code
Var a = "123abc ";
A. valueOf = function () {return parseInt ();}
Alert (++ a); NaN
Alert (a-0); NaN

3. a is of the object type.

The Code is as follows: Copy code
Var a = new Object ();
A. toString = function () {return "123abc ";}
A. valueOf = function () {return parseInt ();}
Alert (++ a); 124
Alert (a-0); 124

4,

The Code is as follows: Copy code
String. prototype. valueOf = function ()
{
Return parseFloat (this );
}
Alert ("123abc"> 122); false
Alert (new String ("123abc")> 122); true

5,

The Code is as follows: Copy code
Var s = new String ("abc ");
Alert (typeof (s) = typeof ("abc"); false
Alert (s = "abc"); false
Alert (s. toString () = s); true

6,

The Code is as follows: Copy code
Var a = new Object ();
A. toString = function () {return ""};
Var B = new Object ();
B. toString = function () {return "B "};
Alert (a> B );
A. valueOf = function () {return 1 };
B. valueOf = function () {return 0 };
Alert (a> B );

7,

The Code is as follows: Copy code
Function step ()
{
Return function (x)
{
Return x + a ++;
}
}
Var a = step (10 );
Var B = step (20 );
Alert (a (10 ));
Alert (B (10 ));

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.