Javascript 各种数据类型的转化
取值方法:
对应的布尔值: !!value
是否==1和true: value == true
是否==0和false: value == false
数据类型对应的布尔值是否==1和true是否==0和false
字符串 'abc'
true
false
false
字符串 '0'
true
false
true
空字符串
false
false
true
带一个空格的字符串
true
false
true
数字1
true
true
false
数字0
false
false
true
数字-1
true
false
false
null
false
false
false
undefined
false
false
false
true
true
true
false
false
false
false
true
可见,能转化为true不代表就==true。
另外:
一、null 和 undefined
null和undefined都会被转化为false,但他们既不==true,也不==false
null == undefined 为true
但是 null === undefined 为false
还有一点,null*1=0 而 undefined*1=NaN
二、上表里任何值都不全等于(===)其他值
三、在所有!=0的值里,true > 0 , 其它都是既不大于也不小于0
暂时就能想到这些了,以后再补充吧
下面是使用到的代码:
function w(thing){ //输出文本
document.write(thing);
}
var desc = ["字符串 'abc'","字符串 '0'",'空字符串','带一个空格的字符串','数字1','数字0','数字-1','null','undefined','true','false'];
var arr1 = ['abc','0','',' ',1,0,-1,null,undefined,true,false];
var value = 0; //下面就是在测试所有值是否与它相等
var result;
w('<table class="data">');
w('<tr><th>数据类型</th><th>是否=='+value+'</th></tr>');
for(i = 0; i<arr1.length; i++){
w('<tr><td>'+desc[i]+'</td>');
result = arr1[i]==value;
w('<td class="'+result+'">'+result+'</td></tr>');
}
w('</table>');
相关文档:
Do you want an ultra fast web site? Get JSL and optimize your JavaScript loading now!
Imagine there's a 4-lane highway between your web browser and the internet itself. This highway is optimize to let pictures, text, and css fly by. But, when it comes to external scripts, the highway crea ......
字符是计算机软件处理文字时最基本的单位,可能是字母,数字,标点符号,空格,换行符,汉字等等。字符串是0个或更多个字符的序列。文本也就是文字,字符串。说某个字符串匹配某个正则表达式,通常是指这个字符串里有一部分(或几部分分别)能满足表达式给出的条件。
......
1.javascript与Html中的调用问题。
在javascript中要想更改html中的一些属性值,特别是在页面内容较多时,务必使用document.getElementById(id)的方式进行,否则javascript将不能更改属性!
2.Date类。
在Date类中的getMonth()等方法返回值为number。因此可以使用数组存储月份信息如month=[“1月”,&ldq ......
s<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档< ......
个人记录在案,以免遗忘。
<!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">
<head>
<title>无标题页</title>
<s ......