JavaScript data types and conversions

Source: Internet
Author: User

JavaScript data types and conversions

JavaScript data type and conversion

JavaScript Data Type

1. Boolean (Boolean)

Boolean: (Value Type) var b1 = true; // Boolean Type

2. Number (Number)

Value: (Value Type) var n1 = 3.1415926; // Value Type

N1.toFixed (3); // The value is rounded to three decimal places.

3. String (String)

Var s1 = 'hello'; // string type

String: (value type, character string immutable)
4. Undefined (Undefined)

Undefined is a value type. It is slightly different from null in the database, for example, numeric calculation or string calculation.

The Undefined and Null types are data types with only one value, which are undefined and null respectively.

5. Null (Null Object)

6. Object (Object type)

Object is a reference type, and others are basic data types.

String is also a basic type. dynamic attributes cannot be added for String, but can be referenced for type.

Reference Type Object instanceof type, judge whether a value is of a certain type, all reference type instanceof Object returns true

7. Application Type

Object: (reference type)

Var tim = new Date (); // object type (object) var names = ['zs', 'Ls', 'ww ']; // array is also an object type (object) var obj = null; // object

Function: (reference type)

Function fun () {}// typeof (fun); // The output result is a function, function Type

PS: view the type of the variable with typeof (variable), see: http://www.lai18.com/content/350151.html

Null and undefined in JavaScript

Undefined, indicating an unknown state

This variable is declared but not initialized. Its value is undefined ). (Access to non-existent properties or object window. xxx) if the method does not explicitly return a value, the return value is undefined. When the typeof operator is applied to undeclared variables, it is displayed as undefined (*)

Null indicates an object that does not exist. null is a special value.

You can assign a value to null to a variable. The value of the variable is "known" (not undefined), that is, null. (Used to initialize the variable, clear the variable content, and release the memory)

Undefined = null // The result is true, but the meaning is different.

Undefined = null // false (*), PS: first judge whether the type is consistent, then determine the value. === Strictly equal ,! = Strictly not equal

Because = will convert the value type before determining whether it is equal, sometimes unexpected results may exist, so we recommend using =. However, in some cases, = can bring better results.

Type conversion

ParseInt (arg) converts a specified string to an integer parseFloat (arg) and converts the specified string to a floating point Number (arg) convert to a number (which can be an integer or floating point number); convert to the whole value, not the part value. If the string cannot be fully converted to an integer, NaN is returned. (Not a Number) isNaN (arg) to determine if arg is a non-digit (NaN), and NaN are Not equal. String (arg) converts a given value (of any type) to a String. Boolean (arg) converts a given value (of any type) to a Boolean value. (*) eval (codeString) calculate and execute the js Code of a string.

The above is the javascript data type and conversion method. I hope you will like it.


Read more about the JavaScript series technical articles and add them to favorites.

1. Simulation of object-oriented technology in JavaScript

2. javascript function hijacking [

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.