Javascript: if (0 =='') condition is true (note the difference between == and ===)

A few days ago when writing javascript script code was found to determine if statements: if (v =='') when v is 0, the result was as true, such as:

<script type="text/javascript"
if(0 == '') {
 document.write(' I love fuck, eggs  <br/>');
if(0 == false) document.write('yes');

Implementation of results:
I speak, eggs pain

Later if (0 == false) document.write ('yes'); is Ricki assume in dealing with different types of data == comparison, the first and then converted to a Boolean type to compare the test, but after I read the help of Javascript documents, found assumption is correct. Such as (the following excerpt from the documentation):

Equality operator (==,!=)
If the two different types of expression, is trying to convert them to strings, numbers, or Boolean quantity.
NaN and including itself does not equal any value.
Negative zero equals positive zero.
null and null and undefined are equal.
The same string, numerically equal to the number, the same object, the same Boolean value, or (when the type is not the same) can be forced into one of the above, are considered to be equal.
Other comparisons were considered not equal.

Identity operator (===,!==)
In addition to not to cast, and the type must be the same, these operators and the role of the equality operator is the same.

If the above change == === First, the results are:

Free, welcome to the red sweet potato stroll

分类:Web 时间:2010-11-29 人气:167
blog comments powered by Disqus


  • javascript import include import the data type check function 2010-09-12

    function isAlien(a) { return isObject(a) && typeof a.constructor != 'function'; } function isArray(a) { return isObject(a) && a.constructor == Array; } function isBoolean(a) { return typeof a == 'boolean'; } function isEmpty(o) { var i, v;

  • Javascript in the browser environment (vi) incident 2009-03-04

    Matter Examples of a lot of events. Such as user input, the click of a button and so on. Can put a javascript function assigned to an event (this can be called the event listener or event handler), when the incident happened, and will perform this fu

  • JavaScript中使用document.write向页面输出内容实例 2013-12-12

    这篇文章主要介绍了JavaScript中使用document.write向页面输出内容实例,本文讲解了输出普通文字.带HTML标签内容的方法,需要的朋友可以参考下 document.write 命令向页面输出文字 本实例使用 JavaScript 的 document.write 命令向页面输出指定的文字,代码如下: <script type="text/javascript"> document.write("我是向页面输出的文字!"); </s

  • javascript中的方法使用介绍 2014-07-27方法打开一个新的文档并用document.write()方法编写文档的内容,下面有个不错的示例,大家可以感受下方法打开一个新的文档,并用document.write()方法编写文档的内容,然后用document.close()方法关闭文档操作,使其内容显示出来. 例如: <script type="text/javascript"> window.onload=hello; function hello(){ v

  • javascript 兼容所有浏览器的DOM扩展功能 2014-10-16

    技术文章写得少,所以有时候想写点什么却下不了手,不知道该写什么:往往到了准备要写的时候才发现自己想写的东西其实很无聊,甚至觉得很幼稚,于是又关掉了编缉器 今天周五,很闲,坐在电脑前没什么事可做,产品线的人也没提什么新的需求,可能下周会有新的需求和工作安排,但那是下周的事了.今天就想写点技术的东西,也就当作是记记笔记,本人水平有限,希望大家多多指教,嘴下留情,哈哈. 有时候我们会想扩展DOM元素的功能,可以添加一些自定义的方法,以让它用起来更加灵活.方便:先来举个例子: <!DOCTYPE htm

  • JavaScript中匿名函数用法实例 2015-01-16

    这篇文章主要介绍了JavaScript中匿名函数用法,实例分析了javascript中匿名函数的功能.定义及具体用法,需要的朋友可以参考下 本文实例讲述了JavaScript中匿名函数用法.分享给大家供大家参考.具体分析如下: JS中可以不用给函数名称,而是在使用时直接通过function把函数体赋值给相关的事件或者变量. 下面的JS代码给按钮设定了一个onclick事件,使用了匿名函数 <form action="#"> <input type="butt

  • 关于JavaScript与HTML的交互事件 2015-02-15

    本篇文章小编将为大家介绍,关于JavaScript与HTML的交互事件,有需要的朋友可以参考一下 JavaScript和HTML的交互是通过事件实现的.JavaScript采用异步事件驱动编程模型,当文档.浏览器.元素或与之相关对象发生特定事情时,浏览器会产生事件.如果JavaScript关注特定类型事件,那么它可以注册当这类事件发生时要调用的句柄. 事件流 事件流描述的是从页面中接收事件的顺序,比如有两个嵌套的div,点击了内层的div,这时候是内层的div先出发click事件还是外层先触发?

  • 第一个JavaScript入门基础 document.write输出 2015-03-27

    关于JavaScript,他是一个OOSP(面向对象脚本语言)他是用来创建动态网站,增强用户界面的一门技术.如果你想了解更多关于JavaScript的信息,请去维基百科查询. 如果你有编程基础,学习Javascript是一件很容易的事情,如果你没有编程基础,也不要担心,我们会为你解释每一行代码. <html> <body> <script type="text/javascript"> document.write("This is my

  • form's submit method and submit the event (onsubmit) 2010-08-31

    form's submit method and submit the event (onsubmit) When the form with the javascript method calls directly to the form when submit, submit events do not respond. Why? This article describes how to automatically submit a problem description When the

  • Then get the html object eval 2010-04-19

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head> <meta http-equiv="Content-

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 闽ICP备15018612号

processed in 0.038 (s). 13 q(s)