The data type and its conversion in JS

Source: Internet
Author: User

The data type in Js

There are six types of data in Js, that is , number,string,boolean, Underfine,null,object.

one,Number

The Number data type refers to numbers, which can be integers or floating-point numbers. As

var a=12,b=12.5;

two,String

A string consists of 0 or more characters, including letters, numbers, punctuation, and spaces ; It is important to note that

the string must be enclosed in quotation marks (single or double quotes);

Such as

var bob= "man"; alert ("bob"); alert (bob);

the browser first pops up a string containing "Bob", and then pops up a string containing "man", which is a direct popup string, and thelatter is the value of the popup variable.

three,Boolean

Boolean data can only have two values of true and false, in js true and False is the Keyword. In general, we use a Boolean value to judge the condition more Often. Such as:

var flag=true;if (flag) {//js code}

In the conditional judgment statement we see all the judging conditions as a Boolean value (here we need to understand some of the conversion characteristics of Boolean values).

four,Underfine

Underfine usually refers to a variable that has no assignment, and the type of the data can be judged by typeof . As

var a,b=underfine;alert (a); Alert (typeof a);

two times the results were underfine.

five,NULL

Null is a special type with only one Value. Represents an empty object reference. Such as:

var a=null;

leave a Empty.

six,Object

An object is a data entity that is composed of some properties and methods that are related to each other. Common objects are an array,window,document , and so On.

Cases:

var today = new Date (), var year = Today.getfullyear (), var month = Today.getmonth () + 1;var day = Today.getday ();

you can invoke the Object's method by creating an instance of the object, as above, creating a Object instance of Date Today, Today the date is obtained by invoking the method of Date.

array array is a frequently used object that is made up of multiple ( key - a Multi-container. Its index is defaulted from 0 start. There are two ways to create an array: 1 Create an instance of an array object var arr=new Array ( ); 2 Direct use of literal-oriented var a=[ [+];

Cases

<script type= "text/javascript" >        var date=new date ();        var day=date.getday ();        var weekly=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]        document.write ("today is" +weekly[day]+ "<br>"); </script>

The result will output today is the day of the Week.

Viewing and conversion of data types

in JS we often need to know the data types of certain variables and convert them to the type of data we Need.

in general, We determine that the data type of the variable will use the identifier typeof, such as:

var mood = "happy"; Alert (typeof  mood); alert (typeof  95);

through identifiers, we can quickly get the type of data ;

In the conversion of data, we often use to convert variables into strings or numbers.

Convert to String to use toString(), Example

var married = False;alert (married.tostring ());

when converting to a number, there are two methods,parseint () converted to integers, andparsefloat () converted to floating-point numbers.

Cases:

var test = parseint ("blue"); Returns Nanvar test = parseint ("1234blue"); Returns 1234var test = parseint ("22.5"); Returns 22var test = Parsefloat ("1234blue"); Returns 1234var test = Parsefloat ("22.5"); Returns 22.5

The data type and its conversion in JS

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.