Detailed description of data types in JavaScript

Source: Internet
Author: User
Tags numeric numeric value string to number

Second, determine what data type a variable belongs to.

1. Numeric type (number).

A numeric expression refers to any expression that has a numeric value. The elements of such an expression can include keywords, variables, literals, and operations
Any combination of characters, as long as this combination can generate a number. In certain cases, if possible, strings can also be
Converted to numbers.


A more commonly used method of judgment is

The code is as follows Copy Code

function Isnumber (val)
{
Return typeof val = = ' number ';
}

But there are some things you can't do. Like what

var A;
document.write (Isnumber (parseint (a)));
The print here is true, but in fact the variable A is Nan, and it cannot be used for numeric operations.
So the above function can be modified to

The code is as follows Copy Code
function Isnumber (val)
{
Return typeof val = = ' number ' && isfinite (val);
}

The Isfinite () function is JS-led, which filters out nan and infinity.
In addition, to determine whether a variable is Nan, you can use isNaN (), which returns a Boolean value.

6 ways to convert numbers to strings

The code is as follows Copy Code
var n = 1.23456;
var n_as_str = n+ "";
String (n);
N.tostring (x); X=2,binary; X=8, Octonay; X=16,hexadecimal.if Empty,decimal
N.tofixed (x); Number of digits after the decimal point
N.toexponential (x); Display exponential form, x denotes decimal place
N.toprecision (x); If n-digit >x is displayed as exponent, x indicates the precision of the number


2. Boolean (Boolean), String, and undefined (Undefined).

These 3 types are relatively simple, direct use

An expression that evaluates to True or FALSE.
Non-Boolean expressions can also be converted to Boolean values, if required, but follow these rules: All objects are treated as true. When and only if the string is empty, the string is treated as false.
Null and undefined are treated as false.
When and only if the number is zero, the number is treated as false.

typeof val = = ' Boolean '
typeof val = = ' String '
typeof val = = ' undefined '

Special data types are: Null Undefined

Null = = undefined
True

"Nan" = = Nan
False

5 = = NaN
False

Nan = = Nan
False

Nan!= nan
True

false = =0
True

true = =1
True

true = = 2
False

undefined = = 0
False

Null = = 0
False

"5" = = 5
True
It's OK.

3. Objects (object) and Null values (NULL).

Because the TypeOf returns object when the variable is null, the object cannot be used directly
typeof judgment. It should be.

The code is as follows Copy Code
function Isobj (str)
{
if (str = = NULL | | typeof str = = ' undefined ')
{
return false;
}
return typeof str = = ' object ';
}

The null value can be judged by val = =. Be careful with congruent.

Object comparison

If it is object, array, function type, compare their reference. True only if their reference are equal.

The code is as follows Copy Code

function Point (x,y) {

this.x = x;

This.y = y;

};

Point.prototype.toString = function () {

alert ("in toString");

Return "x=" + this.x + "y=" + this.y;

};

 

Point.prototype.valueOf = function () {

alert ("in valueof");

Return this.x+this.y;

};

var pa = new Point (1,1);

var PB = new Point (1,1);

var pc = PA;

Then: PA!=PB;

PA!==PB;

Pa==pc;

Pa===pc;

 

var arr1 = [1,2,3];

var arr2 = [1,2,3];

Arr1!=arr2, ARR1!==ARR2

 

 

Have to say 0, false, null, undefined

var t1 = 0;

var t2 = false;

var t3 = null;

Var T4;

Then: t1==t2;t1!==t2;

T1!=t3; t1!==t3;

T1!=t4; t1!==t4;

T2!=t3; t2!==t3;

T2!=t4; t2!==t4;

T3==t4; t3!==t4;

3. Compare the conversion order of objects

If an object is compared to a basic type, the valueof of the objects is invoked first, and then the toString of the object is compared to the base type. That is, turn to the number type first, then the string type.

If it's a Boolean comparison, first convert true to 1,false to 0 and then compare.

var pa = new Point (1,1);

alert (pa==2); Will output "in valueof" and then output "true";

If the Point.prototype.valueOf is shielded, the output "in toString" and then the output "false";

var pa = new Point (1,0);

Then pa==true;

Relational operators >=,<=,>,<

If both sides are numbers, or can be converted to numbers, compare numbers.

If both sides are string, or can be converted to string, the string is compared.

If one side can be converted to a string and the other is a number, then try to convert string to number and then NaN, which returns False if string cannot be converted to number.

4. Arrays (array).

Array types cannot be judged by typeof. Because when the variable is an array type, TypeOf returns object.
Here are two ways to determine the type of an array.

The code is as follows Copy Code

function IsArray (arr)
{
return Object.prototype.toString.apply (arr) = = ' [Object Array] ';
}
Or
function IsArray (arr)
{
return arr.constructor = = Array;
}

NaN: Reserved word (indicates that the data type is not a number)

Undefined: An object property or method does not exist, or a variable is declared but never assigned a value.
That is, when you use an object's undefined attribute or undefined method or when you declare a variable, but you never assign it, you
It operates (except for the assignment), and there is a "undefined" hint that a null value indicates that a variable does not contain valid data.

The reason for NULL is that the value is explicitly assigned to NULL for a variable. Any action between an expression that contains null.

Boolean expression
An expression that evaluates to True or FALSE.
Non-Boolean expressions can also be converted to Boolean values, if required, but follow these rules: All objects are treated as true. When and only if the string is empty, the string is treated as false.
Null and undefined are treated as false.
When and only if the number is zero, the number is treated as false.

String comparisons
A comparison between a sequence of two characters.
All string comparison operations are binary unless indicated in the function that compares the operation.
In English, binary comparisons are case-sensitive, while text comparisons are indistinguishable.

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.