下面是小实验案例

// 0
if(undefined) {
console.log('1');
} else {
console.log('0');
} // 0
if(null) {
console.log('1');
} else {
console.log('0');
} // 0
if('') {
console.log('1');
} else {
console.log('0');
} // 0
if(0) {
console.log('1');
} else {
console.log('0');
} // 1
if('0') {
console.log('1');
} else {
console.log('0');
} // 0
if(false) {
console.log('1');
} else {
console.log('0');
} // 1
if('false') {
console.log('1');
} else {
console.log('0');
} var json = {};
// 1
if (json) {
console.log('1');
} else {
console.log('0');
} // 0
if (json.val) {
console.log('1');
} else {
console.log('0');
} var val;
// 0
if (val) {
console.log('1');
} else {
console.log('0');
} var arr = [];
// 1
if (arr) {
console.log('1');
} else {
console.log('0');
} // 0
if (arr[0]) {
console.log('1');
} else {
console.log('0');
} if (undefined_val) { // 报错
console.log('1');
} else {
console.log('0');
}

通过案例,我们能知道:

  1. undefined,null判断结果是false
  2. 0,false判断是false
  3. ‘0’,‘false’判断是true
  4. {},[]空json,空数组判断是true
  5. 空json中的值,空数组中的值判断是false
  6. 定义的变量没有赋值的,判断是false
  7. 未定义的变量会报错

方法论:这些都是单元测试,经常做些单元测试比较,能够更好的更灵活的更清晰的运用知识。

js的一些常用判断小实验的更多相关文章

  1. 理解JS闭包的几个小实验

    学了JavaScript有一段时间了,但是对闭包还是不太理解,于是怀着心中的疑问做了几个小实验,终于有点明白了. 首先看一下MDN上的定义:闭包是函数和声明该函数的词法环境的组合. 简单来说,闭包是一 ...

  2. JS开发中常用的小技巧

    1.获取指定范围内的随机数 1 2 3 function getRadomNum(min,max){     return  Math.floor(Math.random() * (max - min ...

  3. js中关于事件捕获与事件冒泡的小实验

    1.事件冒泡:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div - ...

  4. js闭包小实验

    js闭包小实验 一.总结 一句话总结: 闭包中引用闭包外的变量会使他们常驻内存 function foo() { var i=0; return function () { console.log(i ...

  5. 3D网页小实验-基于Babylon.js与recast.js实现RTS式单位控制

    一.运行效果 1.建立一幅具有地形起伏和不同地貌纹理的地图: 地图中间为凹陷的河道,两角为突出的高地,高地和低地之间以斜坡通道相连. 水下为沙土材质,沙土材质网格贴合地形,河流材质网格则保持水平. 2 ...

  6. js,jQuery数组常用操作小结

    一.js中数组常用操作小结 (1) shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift() ...

  7. Oracle常用知识小总结

    永不放弃,一切皆有可能!!! 只为成功找方法,不为失败找借口! Oracle常用知识小总结 1. 创建自增主键 对于习惯了SQL SERVER的图形化界面操作的SQLer,很长一段时间不用oracle ...

  8. html+css+js实现狼吃羊小游戏

    html+css+js实现狼吃羊小游戏 一.总结 一句话总结:给动的元素下标记,这里表现为将要活动的标签动态增加class,这是一种很好的思想. 1.如何实现棋子走动的时候简单精确定位? 用重构坐标系 ...

  9. 页面加载完毕后调用js方法进行布局操控 已实验

    页面加载完毕后调用js方法进行布局操控 已实验 $(function(){ var check1 = $("[id$=SMS]").is(':checked'); var bl=$ ...

随机推荐

  1. vue 连接后台

    在 index.js 中可以配置后台的地址:代理的方式: 这个文件在 config 中 proxyTable: { // 连接后台 '/api':{ target:"http://new.w ...

  2. STM32中assert_param的使用

    在STM32的固件库和提供的例程中.到处都能够见到assert_param()的使用. 假设打开不论什么一个例程中的stm32f10x_conf.h文件,就能够看到实际上assert_param是一个 ...

  3. 浅析为什么 char 类型的范围是 : -128~+127

    在 C 语言中. signed char 类型的范围为 -128~127,每本教科书上也这么写.可是没有哪一本书上(包含老师)也不会给你为什么是 -128~127,这个问题貌似看起来也非常easyea ...

  4. LeetCode 之 Merge Sorted Array(排序)

    [问题描写叙述] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

  5. php 数组元素高速去重

    1.使用array_unique方法进行去重 对数组元素进行去重.我们通常会使用array_unique方法,使用这种方法能够把数组中的元素去重. <?php $arr = array(1,1, ...

  6. SQLSever: 怎样在select中的每一行产生不同的随机数?

    select 的随机函数有点假, 或许是由于它是基于时间来的吧, 同一select中由于时间无法错开导致产生的随机数都是一样的. 怎样做到让不同的行拥有不同的随机数呢? 以下以产生某个月的随机日期来演 ...

  7. hadoop 多文件夹输入,map到reduce怎样排序

    使用MultipleInputs.addInputPath 对多个路径输入 如今如果有三个文件夹,并使用了三个mapper去处理, 经过map处理后,输出的结果会依据key 进行join, 假设使用T ...

  8. less01

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. zzulioj--1831-- 周末出游(vector建图+dfs)

    1831: 周末出游 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 22  Solved: 8 SubmitStatusWeb Board Descr ...

  10. hdoj--5569--matrix(动态规划)

    matrix Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...