Home > Web Front-end > JS Tutorial > The difference and connection between the two caused by undefined==null Page 1/3_javascript skills

The difference and connection between the two caused by undefined==null Page 1/3_javascript skills

PHP中文网
Release: 2016-05-16 19:07:15
Original
1014 people have browsed it

It seems that I have seen null==undefined and null!==undefined before, but I never paid attention to them.

Recently I was looking at someone else’s program source code, and there were some judgments like if(x!=undefined&&x!=null&&x!="") everywhere. I suddenly felt that the writing was very verbose. I am usually direct. if(x){}because null, undefined, "" are actually false in conditional judgment, why is it so verbose? (However, please note that if your x==0 is also a legal value, you cannot use if(x){} to make conditional judgments)

In order to prove this redundancy, I also specially did the following test:
var a;
alert(x)
if(x==null){
alert('failed')
}

where x is an uninitialized Variable, that is, undefined.
Run the above code: undefined and failed pop up successively, indicating that x==null is qualified.

We can directly alert(null==undefined) and actually find that true is returned.

This all shows that writing such as x==null||x==undefined or x!=null&&x!=undefined is completely redundant!

Generally, you only need to judge null or undefined.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template