JavaScript common data types and data conversion

Source: Internet
Author: User

First, the original data type: string, number, Boolean, null, Undefined, object. (Simple use (typeof xx) to judge)

1) Basic Data type:

A) string (string)

Data base types (arguments) and reference types

1. Define a string of basic data type var s = "xiao.ming";

2. Define a reference type for the basic data var s = new String ("xiao.ming");

Defines the property of a data type S.name = "Ming";

Defines a method that has a data type s.age = function () {return 20;};

At the time of reference: alert (box);
Alert (box.substring (2)); System built-in objects, all valid
alert (box.name); Custom methods: 1 invalid, 2 valid
Alert (Box.age ()); Custom methods: 1 invalid, 2 valid

Common methods of basic data types

Alert (S.chatat (1)); I

Alert (S.indexof ("I"));//2;//Search from the beginning of the T position
Alert (S.lastindexof (' t '));//3;//search for the position of T from the tail

String also has a method of manipulating strings, contact (' is ', ' teacher '), slice 1 to 2 characters, substr (2,4) from the second
Start selecting four characters, substring 1 to 2;


Difference
S.slice (-2); method of calculation; Total string length 2 = 6; so S.slice ( -2) = = S.slice (6);
S.substring (-2), if it is a negative number. Return all strings

2) Number

var n = 100;

eg: static type
alert (N.max_value); Undefined
Alert (number. max_value); static type of//number

Common methods for Number objects
Alert (box.tostring ()); Box is a number type and is now a string type;
Alert (box.tolocalesring ()); Localization
Alert (box.tofixed (2)); The decimal point is reserved two bits and rounded

Note: Other static methods and basic types of methods you can find the mother.

3) Boolean

Boolean has only two logical values: true| | False

var x = "HI";

var Xasboolean = Boolean (HI);

When this is defined, you can use the following judgment

if (x) {alert ("value is true!");};

4) null and undefined

var x = "";//null, and null is the keyword

var y;//no type;

Eg: A family of three people are in the house at this time: .... Then 2 went out, at this time for this room, 2 bit null, if 4, is undefined, is not there;

5) Composite data type Object

Here is a simple understanding of object, it has a lot of knowledge.

The simple understanding of object is: unordered collection of Members

var person={firstname: "Bill", LastName: "Gates", id:5566};

|| var person = new Object;p erson={firstname: "Bill", LastName: "Gates", id:5566};

Read person value

var name = Person.firstname | | var name = person["FirstName"];

Second, data conversion

Tonumber: Convert to Number type
ToString: Convert to String type
ToBoolean: Convert to Boolean
Toobject: Convert to type Object
Toprimitive: Convert to original type

JavaScript common data types and data conversions

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.