JavaScript data type-related knowledge points

Source: Internet
Author: User

(1) Basic Data type Introduction

JavaScript data types fall into two categories: primitive type (primitive type) and object type

Or, you can have a type of method and a type that cannot have a method

Or, variable (mutable) type and immutable (immutable) type

Primitive types mainly include: number, String, Boolean (Boolean), undefined (generally undefined values and definitions unassigned as undefined )

Where Nan belongs to a special number

Object type the blunt data type is: objects (object)

Where null belongs to a special object

Object is a collection of properties, and basically each property is composed of Key/value

Where a class can be considered to be a subtype of an object type, mainly: array, function, date (date), regular (REGEXP), error (ERR) class

Look at the code:

 varA1;varA2 =true; vara3 = 1; vara4 = "Hello"; varA5 =NewObject ();varA6 =NULL; varA7 =NaN;varA8 =undefined; alert (typeofa);//Show "undefined"AlerttypeofA1);//Show "undefined"AlerttypeofA2);//Show "Boolean"AlerttypeofA3);//Show "Number"AlerttypeofA4);//Show "string"AlerttypeofA5);//Show "Object"AlerttypeofA6);//Show "Object"AlerttypeofA7);//Show "Number"AlerttypeofA8);//Show "undefined"

By simple comparison, the null and undefined values are equal, but not congruent;

Nan is not equal to any value, including itself. If you want to determine if the variable x is NaN, through the method X==nan does not work, you should use x!=x judgment, of course, you can also use the function isNaN (x) to determine

function isNaN () returns True if the argument is Nan or is a non-numeric value (such as a string or an object)

In addition, Infinity represents infinity, with the corresponding function to determine isfinite (), parameters if not Nan, Infinity, or-infinity return True

The negative 0 value is special, and the positive zero value is equal, but it is different as a divisor:

var // is var // Negative // true // false

In addition, JavaScript is a programming language that uses binary floating-point numbers, so there may be problems when calculating floating-point numbers, which should be avoided: for example,

var z =. 4-. 3; Console.log (z);    // 0.10000000000000003 var x =. 3-. 2; console.log (x);    // 0.09999999999999998 var y =. 2-. 1; Console.log (y);    // 0.1

Simple comparison

 varA1;//the value of A1 is undefinedvarA2 =NULL; varA3 =NaN; alert (A1= = A2);//Show "true"Alert (a1! = A2);//Show "false"alert (A1 = = = A2);//Show "false"alert (a1 = = A3);//Show "false"Alert (a1! = A3);//Show "true"alert (A2 = = A3);//Show "false"alert (a2! = A3);//Show "true"Alert (a3 = = A3);//Show "false"Alert (a3! = A3);//Show "true"

you can have a method : That is, you can have a type of method, such as an array with a sort of method: Arr.sort ()

In general, objects, numbers, strings, and Boolean values can have methods

cannot have method : null undefined, etc.

mutable type : means that a value can be modified, such as an array, or an object, that a JavaScript program can change the value of an object's property values and array elements.

var o = {x:1= 2;  // Change o.y = 3;  // Add

Immutable types : Numbers, strings, booleans, nulls, undefined and the like are immutable

For example, a string is an immutable type: (Generally, a new value is returned, and the original value is unchanged)

var str = "You"//Your/ /You

JavaScript variables are untyped (untyped), meaning that variables can be given different types of values.

What are the global properties? such as undefined, Infinity, NaN

What are the global functions? such as isNaN (), parseint (), eval ()

What are the global objects? Like math, JSON

What are the constructor functions? such as date (), RegExp (), String (), Object (), Array ()

(2) The difference between null and undefined

Console.log (number (null//  NULL conversion to 0console.log (5+null);       // 5console.log (number (undefined));  // undefined converted to Nanconsole.log (5+undefined);        // NaNconsole.log (null = = undefined);  // trueconsole.log (null//false

Null means no object, there should be no value here typical usage is:
1. As a function parameter, the parameter of the function is not an object

2. As the end point of the object prototype chain

Like what:


Undefined indicates a missing value, there should be a value here, but there is no typical usage defined:
1. When a variable is declared but not yet assigned, it is equal to undefined

2. When the function is called, the parameter that should be supplied is not provided, and the parameter is equal to undefined

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

4. When the function does not return a value, the default return undefined

Like what:

var//  undefinedfunction//  undefinedvar   new//  undefinedvar x =// undefined

(3) Data type conversion

The JavaScript value type is very flexible, the type conversion is also very flexible, to grasp well, not to say it is very easy

(This section is excerpted from: )

1) Convert to Boolean value

The program is in the IF statement and | |, &&,! The expression is automatically converted to a Boolean value in the context of logical judgment.

There are two ways to manually convert something to a Boolean value:

(1), use!! ; for example Console.log (!!) 30); True

(2), use Boolean (), remember not to add new, such as Console.log (Boolean (30)); True

1. Converts a number to a Boolean value

Except 0 and nan are converted to false, all the numbers from him will be converted to true

2. String converted to Boolean value

This is simpler, except that the empty string is converted to false, and all strings are converted to true

3. Convert other types to Boolean values

Undefined and null will be converted to false, and any objects (including arrays) and functions will be converted to true, remembering that any

var New Boolean (false); alert (o); // converted to a string, the result is false // converts to a Boolean value, the result is true

2) Convert to String

There are two ways to cast a certain thing into a string:

// method One, add it with an empty string // method Two, using a string constructor without new

1. Convert numbers into strings

This is nothing to say, the numbers are converted to strings as they are, but the numbers represented by scientific notation (that is, with E) are converted into strings of real numbers that represent them internally.

It is also important to note that when you use the two-dollar plus operator, if one of the two operands is not a number, the string connection operation is performed instead of the mathematical addition operation, and two operands are converted to a string. When NULL is added to a number, it does not concatenate strings, but instead converts null into a mathematical element.

[]+1+3  // result is[1]+3   // result isnull+1+3  //  Result is 4

2. Convert other types into strings

When an object or function is converted to a string, their toString () method is called to convert, by default Object.prototype.toString and

Function.prototype.toString, but they can be overridden by our own custom toString method. When a function is converted into a string, it is not
Be sure to show the source code of the function, the result of the Function.prototype.toString method depends on how it is implemented by its environment.

3) Convert to Digital

Mathematical operators other than the plus sign perform operations that convert to numbers. There are two ways to force a thing into a number:

// using a unary plus operator, this is the quickest way // another form Number (x)  // convert using a numeric constructor without new

1. Convert strings into numbers

In addition to the empty string will be converted to 0, if the string is the correct form of digital writing, then can be smoothly converted to the corresponding number, whether it is a decimal, scientific count or octal, 16 binary form, and so on. But if you get mixed up with something else that doesn't make a number or does not conform to the rules of digital writing, it will be converted to Nan.

Nan refers to the meaning of not numbers, and any numeric number that is calculated with Nan is Nan,nan or even not equal to itself.

2. Convert other types into numbers

Objects and functions are always converted to Nan, and undefined are converted to Nan, but Null is converted to 0

The table above omits the case of the array. The array is first converted to a string and then converted to a number.

Alert (+[]);  // result is 0 // result is 1 // result is Nanalert (+new// result is 0alert (+new// result is Nan

3. Digital conversion function

parseint, for example

// 123 // NaN


Schedule (authoritative guide)

JavaScript data type-related knowledge points

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: 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.