【JavaScript的五种基本数据类型及转换】
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的五种基本数据类型及转换】的更多相关文章
- javascript中五种基本数据类型
前言: JavaScript中有五种基本数据类型(也叫做简单数据类型)分别为:undefined.null.bolean.number.string:另外还含有一种复杂的数据类型:object. 深入 ...
- JavaScript 基础(一) - JavaScript的引入方式,JavaScript 变量命名规则,JS 的五种基本数据类型,ECMAScript 算数运算符,逻辑运算符
JavaScript的引入方式 直接编写 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- ASP.NET,C#后台调用前台javascript的五种方法
C#后台调用前台javascript的五种方法 由于项目需要,用到其他项目组用VC开发的组件,在web后台代码无法访问这个组件,所以只好通过后台调用前台的javascript,从而操作这个组件.在网上 ...
- js五种基本数据类型:string, number, boolean, null, undefined
/** * 五种基本数据类型:string, number, boolean, null, undefined */ // undefined // 声明变量foo,未声明变量bar var foo; ...
- javascript中6种基本数据类型详解
javascript中有5中数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number和String,还有一种复杂数据类型——object,object本质是由一组键值 ...
- 【转】 C#后台调用前台javascript的五种方法
第一种,OnClientClick (vs2003不支持这个方法)<asp:ButtonID="Button1" runat="server" Te ...
- Java中几种常用数据类型之间转换的方法
Java中几种常用的数据类型之间转换方法: 1. short-->int 转换 exp: short shortvar=0; int intvar=0; shortvar= (short) in ...
- C#后台调用前台javascript的五种方法小结
第一种,OnClientClick (vs2003不支持这个方法) <asp:Button ID="Button1" runat="server" Tex ...
- redis的五种基本数据类型
redis基本数据类型 redis一共分为5中基本数据类型:String,Hash,List,Set,ZSet 第一种String String类型是包含很多种类型的特殊类型,并且是二进制安全的.比如 ...
随机推荐
- [原创]mysql的zip包如何在windows下安装
今天在尝试zipkin的链路数据写入mysql,本机恰好没有按照mysql.找到一个很久前谁发的mysql-5.6.19-winx64.zip,版本不新?别挑剔啦,只是本机测试,能用就好哈哈.. 解压 ...
- mongodb 复制集
mongodb 复制集 复制集简介 Mongodb复制集由一组Mongod实例(进程)组成,包含一个Primary节点和多个Secondary节点,Mongodb Driver(客户端)的所有数据都写 ...
- 网友"就像一支烟"山寨币分析估值方法
[注:素材取自QQ群,2017年12月28日的聊天记录."就像一支烟"分享了自己的山寨币分析估值方法.因为删去了其他人的聊天记录,部分文字可能断章取义,仅供参考,具体情况请自行分析 ...
- iOS动态性:动态添加属性的方法——关联(e.g. 向Category添加属性)
想到要如何为所有的对象增加实例变量吗?我们知道,使用Category可以很方便地为现有的类增加方法,但却无法直接增加实例变量.不过从Mac OS X v10.6开始,系统提供了Associative ...
- 同时只允许Count个线程访问同一块区域的实现方式
转载请注明出处. 好吧,后来才发现有Semaphore和SemaphoreSlim这两个类. 以前的答案: 最近.Net项目中用到了网页截图功能,这个截图功能是类似后台开了一个IE浏览器默默加载某个网 ...
- 西门子flexable创建画面
一.wincc flexable 创建画面包括以下四点 二.具体操作 1.组态画面模板 1)使用该模板的画面包括该模板的所有组件,一个模板也是一个画面 2)给模板上添加一个文本域如下图,则画面1也会显 ...
- Find the Maximum sum
Given an array of n elements.Find the maximum sum when the array elements will be arranged in such w ...
- 基于Docker的ELK日志平台搭建
1.安装Docker Docker可简单理解为一个轻量级的虚拟机.Docker对进程进行封装隔离,隔离的进程独立于宿主和其它的隔离的进程,因此也称其为容器.Docker和传统虚拟化方式的不同.传统虚拟 ...
- Maven项目不打包*.hbm.xml文件
<build> <finalName>basic</finalName> <plugins> <plugin> <groupId> ...
- 快速搭建vsftp 服务器并配置指定目录
1 搭建vsftp 服务器 前期准备: 1.用root 进入系统 2.使用命令 rpm -qa|grep vsftpd 查看系统是否安装了ftp,若安装了vsftp,使用这个命令会在屏幕上显示vs ...