检测js的数据类型

检测js的数据类型

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>检测js的数据类型</title>
</head>
<body>

</body>
</html>
<script type="text/javascript">
    document.write('<h1>数据类型的检测</h1>');
    var mun01,mun02,mun03,mun04,mun05;
    mun01 = 123;
    mun02 = '123';
    mun03 = true;
    mun04 = null;
    document.write('mun01的数据类型:' + typeof(mun01) +'<br/>');
    document.write('mun02的数据类型:' + typeof(mun02) +'<br/>');
    document.write('mun03的数据类型:' + typeof(mun03) +'<br/>');
    document.write('mun04的数据类型:' + typeof(mun04) +'<br/>');
</script>
  1. js的五大数据类型
    1.1、数值型– number 例如: 1 2 1.2 36
    1.2、字符串型 string 例:‘呵呵’ ‘abc’ ‘123’注意:字符串类型的数据如果想相加,那么就相当于把两个字符串做了一次简单的连接操作 ‘avb’+ ‘ba’ = ‘avbba’
    1.3、布尔型– boolean 例: true false 只要这两个值代表的是真和假
    1.4、对象型– object 例: 这是一种复杂数据类型,默认值是null
    1.5、未定义 undefined 代表没有定义 它只有一个值,就是它自己
  2. 检测数据类型
    2.1 typeof(num01) 这会告诉我 num01是五大数据类型中的那一个
  3. 字符转换成数值
    3.1 parseInt(num01) 使用此方法可以把字符串转换成数值型
    4.js在页面中写标签代码
    4.1 document.write(‘这里写什么就会被显示什么,如果写html标签,这个标签就会被浏览器正常的识别出来’)

在页面显示为:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_41485882/article/details/82345996