js the data type conversion, and

Js Data Type

Js in a total of six data types that number, string, boolean, underfine, null, object.

A, number

Number Data type refers to the number that can be integer, and may be floating. Such as

1
var  a=12,b=12.5;

Two, string

String composed of zero or more characters, the characters including letters, numbers, punctuation marks and spaces; Note that

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

Such as

1
2
3
4
5
var  bob=”man”;
 
alert(“bob”);
 
alert(bob);

First, the browser will pop up that contains the string has "bob", and then there are the pop-up containing the string "man", the former is a direct pop-string, which is the value of a variable pops up.

Three, boolean

Boolean data type can only have two values ​​true and false, in the js true and false are keywords. Usually, we judge to set conditions for more than a Boolean value. Such as:

1
2
3
4
5
6
7
var  flag= true ;
 
if (flag){
 
//js 代码
 
}

In the conditional statement, we will all determine the conditions seen as a Boolean value (here need to understand some of the characteristics convert Boolean values).

Four, underfine

underfine generally refers unassigned variables can be determined by the type of data typeof. Such as

1
2
3
4
5
var  a,b=underfine;
 
alert(a);
 
alert( typeof  a);

Two results are underfine.

Five, null

null is only a special type of a value. It represents a null object reference. Such as:

was a = zero;

Will be a empty.

Six, object

It is a collection of objects related to each other by a number of properties and methods of data together to form an entity. Common objects have array, window, document and so on.

Example:

1
2
3
4
5
6
7
var  today =  new  Date();
 
var  year = today.getFullYear();
 
var  month = today.getMonth() + 1;
 
var  day = today.getDay();

 By creating an object instance can call the object, the above is to create a Date object instance today, today come to present information such as the date by calling a method of Date.

Array An array is often used as an object, by a plurality of - more than one container (key value) thereof. Its index of default is zero. Create an array of two ways: Creating an array of object instances var arr = new Array (1,2,3); 2 directly faces literal var a = [1,2,3];

Case

1
2
3
4
5
6
7
8
9
10
11
<script type= "text/javascript" >
 
         var  date= new  Date();
 
         var  day=date.getDay();
 
         var  weekly=[ "星期天" , "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六" ]
 
         document.write( "today is " +weekly[day]+ "<br>" );
 
</script>

The results will be output today is the day of the week.

 

Check the data type conversion

 

In js, we often need to know the data type of certain variables, and converts it into data types we need.

 

Typically, the data type of the variable we will use to determine the identifier typeof, such as:

 

1
2
3
4
5
var  mood =  "happy" ;
 
alert( typeof   mood);
 
alert( typeof   95);

 

By identifiers, we can get the type of data very quickly;

 

Conversion data, we often use is to convert a variable to a string or a number.

 

Converting a string to be used toString (), Example

 

1
2
3
4
var  married =  false ;
 
alert(married.toString());
  

 

When converted to digital, there are two methods, the parseInt () is converted into an integer, parseFloat () converted to floating point.

 

Example:

 

1
2
3
4
5
6
7
8
9
var  test = parseInt(“blue”);  //returns NaN
 
var  test = parseInt(“1234blue”);  //returns 1234
 
var  test = parseInt(“22.5”);  //returns 22
 
var  test = parseFloat(“1234blue”);  //returns 1234
 
var  test = parseFloat(“22.5”);  //returns 22.5

Js in a total of six data types that number, string, boolean, underfine, null, object.

A, number

Number Data type refers to the number that can be integer, and may be floating. Such as

1
var  a=12,b=12.5;

Two, string

String composed of zero or more characters, the characters including letters, numbers, punctuation marks and spaces; Note that

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

Such as

1
2
3
4
5
var  bob=”man”;
 
alert(“bob”);
 
alert(bob);

浏览器首先会弹出包含有“bob”的字符串,然后弹出包含有“man”的字符串,前者是直接弹出字符串,后者则是弹出变量的值。

三,boolean

布尔型数据只能有两种值 true 和 false,在js中true和false是关键字。通常,我们设置布尔值时多用于条件的判断。如:

1
2
3
4
5
6
7
var  flag= true ;
 
if (flag){
 
//js 代码
 
}

在条件判断语句中我们将所有的判断条件看做一个布尔值(这里需要了解一些布尔值的转换特性)。

四,underfine

underfine通常指的是没有赋值的变量,通过typeof可以对数据的类型进行判断。如

1
2
3
4
5
var  a,b=underfine;
 
alert(a);
 
alert( typeof  a);

两次结果都为underfine。

五,null

null是一个只有一个值的特殊类型。表示一个空对象引用。如:

var a=null;

将a清空。

六,object

对象就是由一些彼此相关的属性和方法集合在一起而构成的一个数据实体。常见的对象有array,window,document等。

例:

1
2
3
4
5
6
7
var  today =  new  Date();
 
var  year = today.getFullYear();
 
var  month = today.getMonth() + 1;
 
var  day = today.getDay();

 通过创建对象实例就可以调用对象的方法了,如上就是创建了一个Date的对象实例today,today通过调用Date的方法得出了现在的年月日等信息。

数组array是作为经常使用的对象,是由多个 (键-值) 所组成的一个多容器。其索引 默认是从0开始的。创建数组有两种方法:1创建数组对象实例 var arr=new Array(1,2,3);2直接使用面向字面量 var a=[1,2,3];

1
2
3
4
5
6
7
8
9
10
11
<script type= "text/javascript" >
 
         var  date= new  Date();
 
         var  day=date.getDay();
 
         var  weekly=[ "星期天" , "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六" ]
 
         document.write( "today is " +weekly[day]+ "<br>" );
 
</script>

结果将输出今天是礼拜几。

 

数据类型的查看与转换

 

在js中我们经常需要知道某些变量的数据类型,并将其转换为我们所需要的数据类型。

 

通常,我们判断变量的数据类型会用到标识符typeof,如:

 

1
2
3
4
5
var  mood =  "happy" ;
 
alert( typeof   mood);
 
alert( typeof   95);

 

通过标识符,我们可以很快获取数据的类型;

 

数据的转换中,我们经常用到的是将变量转换成字符串或数字。

 

转换成字符串要使用toString(),例

 

1
2
3
4
var  married =  false ;
 
alert(married.toString());
  

 

转换成数字时,有两种方法,parseInt() 转换成整数,parseFloat()转换成浮点数。

 

例:

 

1
2
3
4
5
6
7
8
9
var  test = parseInt(“blue”);  //returns NaN
 
var  test = parseInt(“1234blue”);  //returns 1234
 
var  test = parseInt(“22.5”);  //returns 22
 
var  test = parseFloat(“1234blue”);  //returns 1234
 
var  test = parseFloat(“22.5”);  //returns 22.5

Guess you like

Origin www.cnblogs.com/xiewangfei123/p/12008934.html