js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Null,Undefined),和一种混合数据类型就是特殊的(Object)。

"undefined" 变量未定义
"boolean" 变量是布尔值
"string" 变量是字符串
"number" 变量是数值
"object" 变量是对象或者null
"function" 变量是函数

typeof  操作符可以检测变量的数据类型(输出的是一个关于数据类型的字符串)。

 //          typeof是得到变量的类型(查看数据类型)
var a; //undefined
a=null; //object
a=; //number
a=NaN; //number
a=""; //string(带引号的基本都是字符串)
a=false; //boolean布尔
a=''; //string(带引号的基本都是字符串)
alert(typeof a);

隐式转换:

以下是一些例子:

其中:

其它类型转换成数值型:
 数值型+undefined=NaN   
数值型+null=数值
布尔boolean:true+2=3 false+2=2
 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
/*
if(exp){
exp为true的代码段;
}else{
exp为false的代码段;
}
*/
//其它类型转换成布尔类型假的有
// var a;//undefined->false
// typeof得到变量的类型
// alert(typeof a);
// a=null;//null->false
// 0 0.0 NaN->false
// a=0;
// a=0.0;
// a=0/0;
// a=NaN;
a=null;
alert(a);
// a='';//空字符串->false
// a='0';
// a=' ';
// alert(typeof a);
// if(a){
// alert('真');
// }else{
// alert('假');
// }
//其它类型转换成数值型
var b=undefined;//undefined->NaN
b=null;//null->0
b=false;//true->1
// b=false;//false->0
// alert(1+b); // 数值型+undefined=NaN 数值型+null=数值 ( boolean:true+2=3 false+2=2)
var c='';//'12'->12
// c='3king';//'3king'->NaN
c=false;
// alert(2*c);
// c='33';
// alert(typeof c);
c=c*;
// alert(typeof c); </script>
</head>
<body>
<h1>隐式转换的例子</h1>
<script type="text/javascript">
//其它类型转换成字符串型
document.write(undefined);//'undefined'
document.write('<br/>');
document.write(null);//'null'
document.write('<br/>');
document.write(NaN);//'NaN'
document.write('<br/>');
document.write();//'123'
document.write('<br/>');
document.write(true);//'true'
document.write('<br/>');
document.write(false);//'false'
document.write('<br/>');
//alert(1+"1");//拼接字符串
//alert('2'+12);//拼接字符串
</script>
</body>
</html>

强制转换:<有五种>

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
//其它类型转换成布尔类型false的有
var test=Boolean();
test=Boolean(-);
test=Boolean(NaN);
test=Boolean(undefined);
test=Boolean('');
test=Boolean(0.0);
test=Boolean('');
//其它类型转换成字符串型
test=String();
test=String(23.34);
test=String('this is a test');
test=String(true);
test=String(false);
test=String(null);
test=String(undefined);
test=String(NaN);
//其它类型转换成数值型
test=Number();
test=Number(232.3);
test=Number(true);
test=Number(false);
test=Number(undefined);
test=Number(NaN);
test=Number(null);
test=Number('3king');
test=Number('');
//通过parseInt()进行转换成整型
test=parseInt('');
test=parseInt(,); //234
// alert(test);
test=parseInt('0xabcdef'); //11295375
//alert(test);
test=parseInt(''); //12344
// alert(test);
test=parseInt(,); //69
// alert(test);
test=parseInt('3ki23ng'); //3
// alert(test); //
test=parseInt('true'); //NaN
//alert(test);
test=parseInt(true); //NaN
//alert(test);
test=parseInt(' 35 6 a ');//35
// alert(test);
//通过parseFloat()转换成浮点型
test=parseFloat('123.34abc');
//test=parseFloat('123');
// test=parseFloat('sdf');
// test=parseFloat(' 2e3a');
alert(test);
</script>
</head>
<body>
<h1>强制转换的例子</h1>
</body>
</html>

【JavaScript的五种基本数据类型及转换】的更多相关文章

  1. javascript中五种基本数据类型

    前言: JavaScript中有五种基本数据类型(也叫做简单数据类型)分别为:undefined.null.bolean.number.string:另外还含有一种复杂的数据类型:object. 深入 ...

  2. JavaScript 基础(一) - JavaScript的引入方式,JavaScript 变量命名规则,JS 的五种基本数据类型,ECMAScript 算数运算符,逻辑运算符

    JavaScript的引入方式 直接编写 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  3. ASP.NET,C#后台调用前台javascript的五种方法

    C#后台调用前台javascript的五种方法 由于项目需要,用到其他项目组用VC开发的组件,在web后台代码无法访问这个组件,所以只好通过后台调用前台的javascript,从而操作这个组件.在网上 ...

  4. js五种基本数据类型:string, number, boolean, null, undefined

    /** * 五种基本数据类型:string, number, boolean, null, undefined */ // undefined // 声明变量foo,未声明变量bar var foo; ...

  5. javascript中6种基本数据类型详解

    javascript中有5中数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number和String,还有一种复杂数据类型——object,object本质是由一组键值 ...

  6. 【转】 C#后台调用前台javascript的五种方法

    第一种,OnClientClick    (vs2003不支持这个方法)<asp:ButtonID="Button1" runat="server" Te ...

  7. Java中几种常用数据类型之间转换的方法

    Java中几种常用的数据类型之间转换方法: 1. short-->int 转换 exp: short shortvar=0; int intvar=0; shortvar= (short) in ...

  8. C#后台调用前台javascript的五种方法小结

    第一种,OnClientClick (vs2003不支持这个方法) <asp:Button ID="Button1" runat="server" Tex ...

  9. redis的五种基本数据类型

    redis基本数据类型 redis一共分为5中基本数据类型:String,Hash,List,Set,ZSet 第一种String String类型是包含很多种类型的特殊类型,并且是二进制安全的.比如 ...

随机推荐

  1. spring boot oauth2的一些记录

    oauth2及时从一个项目A申请另一个项目B的访问的时候,不用在项目A输入项目B的用户名和密码,个人理解先跳转到项目B,利用项目B的用户名和密码得到一个code之类的,这里有点像openID,不过不是 ...

  2. 【python】元组

    Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32Type & ...

  3. C C语言中关键词,以及知识点复习

    C语言学习 C语言练习知识点 auto        局部变量(自动储存) break       无条件退出程序最内层循环 case        switch语句中选择项 char         ...

  4. lograted日志切割脚本

    root@op-testsetup-web3.idc1.yiducloud.cn:/etc/logrotate.d# cat etcd /home/work/docker/logs/etcd/prev ...

  5. xamarin android menu的用法

    在Android中的菜单有如下几种: OptionMenu:选项菜单,android中最常见的菜单,通过Menu键来调用 SubMenu:子菜单,android中点击子菜单将弹出一个显示子菜单项的悬浮 ...

  6. mysql5.7-Group Replication

    什么是Group Replication 基于组的复制(Group-based Replication)是一种被使用在容错系统中的技术.Replication-group(复制组)是由能够相互通信的多 ...

  7. 模板引擎(smarty)知识点总结三

    阔别了几个月,小杨又来分享php知识.话不多说,言归正传,今天继续带来smarty的知识点. -----------------smarty  assign append 详解 对于这两个的区别和联系 ...

  8. NumPy学习笔记 三 股票价格

    NumPy学习笔记 三 股票价格 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.&l ...

  9. 优化设计提高sql类数据库的性能

    前言 在一个项目中,技术的统一性是最重要的,数据库的设计则是重点中的重点.NoSQL 是目前最流行的数据库,但是其实用性和功能性远不如sql数据库. 实际很多SQL数据库被诟病的性能问题大多是源于程序 ...

  10. php SeasLog使用以及liunx环境下安装

    1.下载SeasLog http://pecl.php.net/package/SeasLog php官方 https://github.com/Neeke/SeasLog 作者的github  2. ...