JavaScript Data Types Learning notes

Source: Internet
Author: User
Tags numeric numeric value

Only the simple data types in 5 are explained here, and the Function and Object are explained in the following chapters.


Kind of basic data type

Number Type
String String type
Boolean Boolean type
Undefined indicates that a variable has not been assigned a value
Null-Empty Object

Number Type

The number type is used to represent integers and floating-point numbers. For example:
var x=100; There is also a special value NaN (Non-numeric not a number).

NaN is used to indicate that an operation that is supposed to return a numeric value does not return a value, so that no error is thrown.

String type

A string, which is a character type, represents a string of characters. Strings can be represented by double quotes ("") and single quotes (' ").

For example:

The code is as follows Copy Code

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

Boolean type

Boolean is a Boolean type with only two values: True (True) and False (false).

Boolean types are often used in process control statements, such as:

The code is as follows Copy Code
var x=true;
if (x) {
Alert ("X=true");
}

Run Code, pop-up warning box, display "X=true".
Undefined type
The value of the Undefined type is only one, which is the Undefined. When a variable is declared but not assigned, its value is undefined. For example:

The code is as follows Copy Code

var name;
alert (name); Show undefined

Click here to call a variable with no value assigned

Null type
The value of a null type is only one, or null. Null represents an empty object.

If the defined variable is ready to be used to save the object in the future, it is best to initialize the variable to null instead of another value.
Dynamic type
JavaScript is a weakly typed language, which means that JavaScript has dynamic types, and the same variable can give different types of values. This is not possible in strongly typed languages such as C, C + +, and Java.

The following assignment is correct:

The code is as follows Copy Code

var demo=1; This is number type
Demo=true; This is a Boolean type
demo= "Hello"; This is a string type

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

The result is: number,undefined;

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

The result is: Undefined,number;

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

The result is: Undefined,number;

The code is as follows Copy Code

var A;

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

You can use THIS.A in C to access global variable A

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


Name 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 > True
11, Infinity > "abc" False
12, Infinity = = NaN False
13, True = = 1 ture
14, New String ("abc") = = "ABC" True
15, New String ("abc") = = "abc" false is exactly the same

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

• Attempts to convert both expression1 and expression2 to numbers.
• If both expressions are strings, string comparisons are made in dictionary order.
• Returns False if one of the expressions is NaN.
• minus 0 equals positive zero.
• Negative infinity is less than any number including itself.
• Positive infinity is greater than any number including itself.
Equality operator (= =,!=)

• If the two expressions are of different types, you will attempt to convert them to a string, a number, or a Boolean amount.
nan is not equal to any value, including itself.
• minus 0 equals positive zero.
null are equal to null and undefined.
• The same string, number equal to numeric, same object, same Boolean value, or (when the type is not the same) can be coerced into one of the above situations, are considered equal.
• Other comparisons are considered to be unequal.
Identity operator (= = =,!==)

These operators are the same as the equality operators, except that they do not have type conversions and the types must be the same.


Name their output.

1,

The code is as follows Copy Code
var a = "123abc";
Alert (typeof (a++)); The string,++ operator was not executed at typeof time
alert (a); NaN

2, A is a string type

The code is as follows Copy Code
var a = "123abc";
a.valueof = function () {return parseint (a);}
alert (++a); NaN
alert (a-0); NaN

3, A is type Object

The code is as follows Copy Code
var a = new Object ();
a.tostring = function () {return "123ABC";}
a.valueof = function () {return parseint (a);}
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 "a"};
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,

  code is as follows copy code
function Step ( A)
{
return function (x)
{
return x + a++;
}
}
var a = step (a),
var B = step (),
Alert (a)),
Alert (b),

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.