Home > Web Front-end > JS Tutorial > js code to determine whether a variable is undefined_javascript skills

js code to determine whether a variable is undefined_javascript skills

WBOY
Release: 2016-05-16 17:48:40
Original
1468 people have browsed it

For example:

if(!myVar01)alert("An error occurred");

// An exception occurs directly in this code because the variable myVar01 is not declared if("undefined" == typeof myVar01)alert("An error occurred");

//Writing like this will prevent exceptions

And: var myVar01; if(undefined == myVar01)alert("An error occurred");

// This code will run correctly if("undefined" == typeof myVar01)alert("An error occurred");

// This code will also run correctly

Conclusion: We use the following method to ensure foolproof if("undefined" == typeof myVar01)alert("An error occurred");

// This code will also run correctly

Of course, judging the validity of data is far more than just that, there is also the judgment of null, whether the number is out of bounds.

Practical application:
We do not define some pages of downlm, but if some pages are defined, such a judgment method may be needed. If it is not defined, it will not be executed.

if("undefined" != typeof downlm){ 
if(downlm=="soft"){ 
document.write('成功'); 
} 
}
Copy after login

Tested perfect.

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template