JavaScript Basics Comb----data types

Source: Internet
Author: User

JavaScript data Types

There are six main categories of JavaScript data types:

Undefined, NULL, string, number, object, Boolean

1.Undefined

---Declare a variable with VAR but not initialized,

Using the typeof operator on an uninitialized variable or a variable that has not been defined will return undefined

Problem: Distinguishing between null object pointers and variables that have not yet been defined

With typeof detection, a variable that has not yet been defined returns a undefined, while an empty object pointer returns an object

2.Null

Logically null represents an empty object pointer

object is returned when using typeof detection

The relationship between null and undefined:

Undefined is derived from null so it returns true when compared with "= ="

TIP: You should assign null to an empty object when declaring it

3.String

Consists of 0 or 16 Unicode characters

Characteristics:

Single and double quotation marks must not be cross-used

Use the. Length property to access the length of the string (the escape sequence represents a character and cannot return a double-byte character length)

Once a string is created, its value cannot be changed, and the original string must be destroyed to change

Escape sequence:

\ nthe line break

\ t tab

\b Spaces

\ r return character

\f Page Breaks

\ \ Slash \

\ ' Single quote '

\ "Double quotation mark"

Type conversions:

1.tostring ()---usage type (number,string,boolean,object), you can use parameters to represent the binary, which is 10 binary by default.

2.String ()----usage type (six big data types)

3.eval ()---evaluates the string expression and returns it as a numeric value

There are two ways to convert a value to a string, one is to use the ToString () method, and the second is to use the transform function string (). Here are some questions to keep in mind:

1, most values have the ToString () method, and null and undefined are not.

2, the ToString () method can also be used for the value of a string type, which returns a copy of the string.

The 3,tostring () method can pass a parameter that represents the cardinality of a numeric value.

    1. var t = 8;
    2. T.tostring (2); 1000

By default, the parameter is 10. To illustrate that the parameter for a non-numeric type (boolean,string, etc.) is set to ToString () is not valid,

    1. var t = "8";
    2. T.tostring (2); 8, returns a copy of the string;

4, any value can use the string () method. It's a process like this

First, if the value has the ToString () method, then the method (no arguments) is used.

Second, that is, the value does not have the ToString () method, which is null returns "NULL" and undefined returns "undefined";

    1. var a=null;
    2. String (a); "NULL"

The original meaning of the Eval function is that the function can execute a string as if it were a JavaScript expression.

Suppose the background output JSON string response the foreground using var json = eval ("(" + Response + ")");

If we're serious, we can add a judgment.

var jsondata = "";

if (typeof (data) = = = "string" && data! = "Null") {if (data = = "") data = "{}"; Jsondata = eval ("(" + Data + ")");

At this point, we think that the role of Eval is to convert JSON string to JS object

Functions that can be implemented with Eval can be implemented in an alternative way such as Json.parse (JSON object parsing), such as new function () (Execution of specific code)

Most of the reasons for not supporting the use of Eval are: 1, poor performance 2, unsafe 3, generate confusing code logic

4.Boolean

True is True, False is false,

True is not necessarily equal to 1,false not necessarily equal to 0

Use Boolean () to convert:

Convert to true--1. Any non-empty string

2. Any non-null value

3. Any non-empty object

Convert to false--1. Empty string

2.NaN and 0

3.null and undefined

5.Number

Binary: Decimal

Octal--Prefix 0

--Effective sequence 0~7

--out of range-0 of the prefix is ignored and subsequent values are counted in decimal

Hex--Prefix 0x

--Effective sequence 0~9 a~f

Floating-point number: at least one digit after the decimal point

Maximum accuracy 17 decimal places

Defect-There is a rounding error that cannot be tested on a specific floating-point number

Nan:not a number (non-numeric)

-any operation involving Nan will return Nan,nan and any numeric value is unequal, including itself.

Detection: IsNaN ()---can be converted to a value of false

---cannot be converted to numeric value true

Numeric conversions: Number()

boolean--(True for 1,false is 0);

null--0;

Undefined--nan;

string--(

Contains only numbers: decimal, if 0 is ignored before

Contains valid floating-point format: Floating-point value, leading 0 is ignored

Contains a valid 16 binary: decimal number of the same size

Empty string: 0

Other formats: NaN

object--(ValueOf (), toString ())

6.Object

Definition: A set of data or functions

Disclaimer: Var o=new Object ()

Properties and methods:

1.Constructor Save the function used to create the current object

2.hasOwnProperty (PropertyName) detects whether a given property exists in the current object instance

3.isPrototypeOf (object) detects if an incoming object is a prototype of another object

4.propertyIsEnumerable (PropertyName) detects if a given property is available For-in statement enumeration

5.toLocalString () returns a string representation of the object that corresponds to the region of the execution environment

6.toString () returns the string representation of an object

7.valueOf () returns the string, numeric, or Boolean representation of an object, usually the same as the ToString () value

JavaScript Basics Comb----data types

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.