一. null VS undefined VS NaN

1. null

定义:null是特殊的object,是空对象,没有任何属性和方法。

document.writeln(typeof null); // object  

值得注意的是: 对象模型中,所有的对象都是Object或其子类的实例,但null对象例外:

document.writeln(null instanceof Object); // false  

2. undefined

定义:undefined是undefined类型的值,未定义的值和定义未赋值的为undefined;无法使用 for/in 循环来枚举 undefined 属性,也不能用 delete 运算符来删除它。

作用

(1)变量被声明了,但没有赋值时,就等于undefined。

(2) 调用函数时,应该提供的参数没有提供,该参数等于undefined。

(3)对象没有赋值的属性,该属性的值为undefined。

(4)函数没有返回值时,默认返回undefined。

var i;
i // undefined function f(x){console.log(x)}
f() // undefined var o = new Object();
o.p // undefined var x = f();
x // undefined

提示:只能用 === 运算来测试某个值是否是未定义的,因为 == 运算符认为 undefined 值等价于 null。
注释:null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。

3. NaN

NaN是一种特殊的number

document.writeln(typeof NaN); // number  

他们三个的值

1. null“等值(==)”于undefined,但不“全等值(===)”于undefined,NaN与任何值都不相等,与自己也不相等:

document.writeln(null == undefined); // true
document.writeln(null === undefined); // false
document.writeln(NaN == null); // false
document.writeln(NaN == undefined); // false
document.writeln(NaN == NaN); // false

2.  null与undefined都可以转换成false,但不等值于false:

document.writeln(!null, !undefined); // true true
document.writeln(undefined == false); // false
document.writeln(null == false); // false

3. null不等于0,但是计算中null会当做成0来处理;undefined计算的结果是NaN;NaN计算的结果也是NaN:

alert(null == 0); // false
alert(123 + null); // 123
alert(123 * null); // 0
alert(123 / null); // Infinity
alert(123 + undefined); // NaN
alert(123 * undefined); // NaN
alert(123 / undefined); // NaN
alert(123 + NaN); // NaN
alert(123 * NaN); // NaN
alert(123 / NaN); // NaN

二. empty VS isset

1. empty

检查一个变量是否为空(变量不存在或它的值等同于FALSE),如果变量不存在empty()不会产生警告。

2. isset

检测变量是否设置,并且不是 NULL

<?php

$var = '';

// 结果为 TRUE,所以后边的文本将被打印出来。
if (isset($var)) {
echo "This var is set so I will print.";
} // 在后边的例子中,我们将使用 var_dump 输出 isset() 的返回值。
// the return value of isset(). $a = "test";
$b = "anothertest"; var_dump(isset($a)); // TRUE
var_dump(isset($a, $b)); // TRUE unset ($a); var_dump(isset($a)); // FALSE
var_dump(isset($a, $b)); // FALSE $foo = NULL;
var_dump(isset($foo)); // FALSE ?>

empty与isset,null与undefined的更多相关文章

  1. null、 is_null() 、empty() 、isset() PHP 判断变量是否为空

    PHP中,在判断变量是否为空的时候,总会纠结应该选用哪个函数,下面列取常用的多种情况,其中1/3经过我的验证,其它来自网络,验证后使用... 使用 PHP 函数对变量 $x 进行比较 表达式 gett ...

  2. empty、isset、is_null的比较

    直接上代码 <?php $a=0; $b='0'; $c=0.0; $d=''; $e=NULL; $f=array(); $g='\0'; $h=' ';//space $i=true; $j ...

  3. empty、isset、is

    直接上代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?php $a=0; $b='0'; $c=0.0; ...

  4. [PHP源码阅读]empty和isset函数

    近日被问到PHP中empty和isset函数时怎么判断变量的,刚开始我是一脸懵逼的,因为我自己也只是一知半解,为了弄懂其真正的原理,赶紧翻开源码研究研究.经过分析可发现两个函数调用的都是同一个函数,因 ...

  5. empty和isset函数详解

    1.empty函数 用途:检测变量是否为空 若变量不存在则返回 TRUE 若变量存在且其值为"".0."0".NULL..FALSE.array().var $ ...

  6. php empty()和isset()的区别

    在使用 php 编写页面程序时,我经常使用变量处理函数判断 php 页面尾部参数的某个变量值是否为空,开始的时候我习惯了使用 empty() 函数,却发现了一些问题,因此改用 isset() 函数,问 ...

  7. php部分---include()与require()的区别、empty()与isset is_null的区别与用法详解

    include()与require()的用途是完全一样的,不一定非得哪个放在最前面哪个放在中间.他们最根本的区别在于错误处理的方式不一样. 1.处理错误的方式: require()一个文件存在错误的话 ...

  8. php中empty(), is_null(), isset()函数区别

    empty(), is_null(), isset()真值表(区别) 我们先来看看这3个函数的功能描述 www.111cn.net isset 判断变量是否已存在,如果变量存在则返回 TRUE,否则返 ...

  9. php中empty()、isset()、is_null()和变量本身的布尔判断区别(转)

    在php脚本中,我们经常要去判断一个变量是否已定义或者是否为空,就需要用到这些函数empty().isset().is_null()和其本身作为参数,下面小段程序做个简要比较 <?php//预定 ...

随机推荐

  1. ef oracle参数化问题

    并非所有变量都已绑定 假如一个sql是这样的 string sql =@" select id from a where date between :StartDate and :EndDa ...

  2. HTML5 Form Data 对象的使用

    HTML5 Form Data 对象的使用  MDN: https://developer.mozilla.org/zh-CN/docs/Web/Guide/Using_FormData_Object ...

  3. Markdown预览功能不可用解决方案

    初学者在使用Markdown时也许会遇到这个问题 原因是电脑缺少一个组件,解决方案很简单,安装上就好了,以下是链接 http://markdownpad.com/download/awesomium_ ...

  4. 转:javascript获取上一访问页面

    原文链接:移动端返回上一页,刚需!document.referrer 详解 全文如下: 返回上一页,在PC端我们可以使用:history.go(-1)或者history.back(),可以正常返回第一 ...

  5. Difference between 'SAME' and 'VALID' padding

    Difference between 'SAME' and 'VALID' padding 'SAME' padding 和 'VALID' padding 的区别 If you like ascii ...

  6. Visualizing CNN Layer in Keras

    CNN 权重可视化 How convolutional neural networks see the world An exploration of convnet filters with Ker ...

  7. 整理JavaScript循环数组和对象的方法

    众所周知,常用的循环语句有for.while.do-while.for-in,forEach以及jQuery中提供的循环的方法:以及ES6中提供的很多用来循环对象的方法如map, 在 Javascri ...

  8. Eigen 学习之块操作

    Eigen 为 Matrix .Array 和  Vector提供了块操作方法.块区域可以被用作 左值 和 右值.在Eigen中最常用的块操作函数是 .block() . block() 方法的定义如 ...

  9. leetCode题解之寻找string中最后一个word的长度

    1.题目描述 返回一个 string中最后一个单词的长度.单词定义为没有空格的连续的字符,比如 ‘a’,'akkk'. 2.问题分析 从后向前扫描,如果string是以空格‘  ’结尾的,就不用计数, ...

  10. Spring 集成 Ehcache 开启缓存

    1.jar包 1.1.slf4j-api-1.6.1.jar 1.2.ehcache-2.7.0.jar 1.3.spring-core-3.2.0.RELEASE.jar 1.4.spring-co ...