JS 中如何判断 null
作者:chilleen 发布时间:2007-12-13 20:28:00
标签:null,js,javascript
先来看看js中的Null类型表示什么?null用来表示尚未存在的对象,常用来表示函数企图返回一个不存在的对象,一般一个未定义的变量在初次使用时的类型就是null。
任何区别Javascript中的Null与Undefined?
以下是不正确的用法:
var exp = null;
if (exp == null)
{
alert("is null");
}
exp 为 undefined 时,也会得到与 null 相同的结果,虽然 null 和 undefined 不一样。
var exp = null;
if (!exp)
{
alert("is null");
}
如果 exp 为 undefined 或者数字零,也会得到与 null 相同的结果,虽然 null 和二者不一样。
var exp = null;
if (typeof(exp) == "null")
{
alert("is null");
}
为了向下兼容,exp 为 null 时,typeof 总返回 object。
var exp = null;
if (isNull(exp))
{
alert("is null");
}
JavaScript 中没有 isNull 这个函数。
以下是正确的用法:
var exp = null;
if (!exp && typeof(exp)!="undefined" && exp!=0)
{
alert("is null");
}
尽管如此,我们在 DOM 应用中,一般只需要用 (!exp) 来判断就可以了,因为 DOM 应用中,可能返回 null,可能返回 undefined,如果具体判断 null 还是 undefined 会使程序过于复杂。
0
投稿
猜你喜欢
- 1、如果之前已经安装我们先卸载一下yum -y remove php*2、由于linux的yum源不存在php7.x,所以我们要更改yum源
- 一个方便的特性是你可以给一个视图指定默认的参数。 这样,当没有给这个参数赋值的时候将会使用默认的值。例子:# urls.pyfrom dja
- 在使用django-rest-framework开发项目的时候我们总是避免不了跨域的问题,因为现在大多数的项目都是前后端分离,前后端项目部署
- 1. yum list installed | grep php 查看安装的php版本mod_php72w.x86_64 7.2.1-1.w
- 在部署一套内网测试环境时,频繁宕机,开机后不断的吃内存,重启apache之后内存占用会不停的上涨,直到swap用完,直到死机,由于是内网环境
- 也就是 addEventListener 和 on 的区别为什么需要 addEventListener?先来看一个片段:<div id
- document.cookie="userId=828"; 如果要一次存储多个名/值对,可以使用分号加空格(; )隔开,
- 当我们使用电脑的时候,不可避免的会碰到重复点击的任务。所以我们可以使用python制作一个简单的连点器进行使用,同时可以提升写代码的水平。第
- 1.GO中包的定义与介绍go中包分为三种:1.系统内置包 2. 自定义包 3.第三方包2. 包管理工具 go mod2.1 自定义包 (可以
- 使用pandas库来读取xlsx格式中的数据。excel中数据:示例代码1:import pandas as pd# data = pd.r
- 自定义图片生成词云图的多种方法有时候我们会根据具体的场景来结合图片展示词云,比如我分析的是美团评论,那么最好的展示方法就是利用美团的logo
- 这是一篇关于使用JScript RuntimeObject(MSDN)调试的文章。虽然这些例子中的大多数在其他浏览器中不能运行,但在IE 5
- 开发一个文件上传共享网站,曾想使用下面的代码实现文件上传的功能: <form enctype="multipart/form
- 相信很多程序员在调试代码时,都用过 print。代码少还好说,如果是大型项目,面对众多 print 的输出结果,可能要头大了。今天推荐一个
- 本文实例讲述了Python实现求解一元二次方程的方法。分享给大家供大家参考,具体如下:1. 引入math包2. 定义返回的对象3. 判断b*
- 示意图:python双向链表实现代码:#!/usr/bin/python# -*- coding: utf-8 -*-class Node(
- 一、什么是上下文管理器我们在处理文件的时候经常看到下面这样的代码,它即是上下文管理器:with open('test.txt'
- 一、数字类型。数字类型按照我的分类方法分为三类:整数类、小数类和数字类。 我所谓的“数字类”,就是指DECIMAL和NUMERIC,它们是同
- 资源React-16.8.*react-router-dom-4.3.*TypeScript-3.5.*webpack-4.*eslint-
- asp分页,是学习使用asp编程经常遇到的问题,也算是一个经典的问题。本文介绍了一个asp分页源代码例子,希望对初学者有所帮助,本程序文件名