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 人气:166
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中使用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入门基础 document.write输出 2015-03-27

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

  • With regard to the implementation of the order of javascript code 2009-02-21

    With regard to the implementation of the order of javascript 1. On the page, JS implementation of the order of the first block in accordance with the order to execute <Script>, such as <Script> alert ( 'first implementation.'); </ Script>

  • nginx gzip duplicate MIME type "text / html" 2010-09-14

    Instruction [# Gzip gzip] [# Gzip_buffers gzip_buffers] [# Gzip_comp_level gzip_comp_level] [# Gzip_min_length gzip_min_length] [# Gzip_http_version gzip_http_version] [# Gzip_proxied gzip_proxied] [# Gzip_types gzip_types] After the upgrade to the l

  • Method of implementation of the string the regular expression 2011-05-19

    match method Type: str.match (reg); With the regular expression exec method is similar to the method also returns a similar array of objects, but also input and index attributes. We define a function to test the following: function matchReg(reg,str){

  • 如何用正则取input type="text"中的value 2014-02-04

    1.如何通过正则取input type="text"中的value呢? 2.如何过滤掉input type="checkbox"的标签? 这样只能达到正则取input type="text"中的value可是不能过滤掉input type="checkbox"的标签! alert [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] alert [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 解决: aler

  • HTML教程:INPUT type=text元素 | input type=text对象 2014-09-30

    INPUT type=text元素 | input type=text 创建一个单行的文本输入控件. 成员表 下面的表格列出了 input type=text 对象引出的成员.请单击左侧的标签来选择你想要查看的成员类型. 标签属性/属性 标签属性 属性 描述 ACCESSKEY accessKey 设置或获取对象的快捷键. ATOMICSELECTION 指定 元素 及其内容是否可以一不可见单位统一选择. AUTOCOMPLETE autocomplete 设置或获取对象的自动完成状态. BEG

  • Boolean type in Oracle implementation 2010-11-26

    Has been no boolean type in Oracle problems, there are two solutions, one with the Number (1), the second is to use Char (1), have their own strengths, individuals prefer to use the Number methods to solve a very simple reason, because starting from

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

processed in 0.227 (s). 12 q(s)