Javascript IsDefined Function

by @jehiah on 2004-04-16 10:02UTC
Filed under: All , Javascript

I recently needed a way to check if a variable was defined in javascript. This function is exactly analogous to the ColdFusion function IsDefined.

<script language="javascript">
// JavaScript Document
/*
// ///////////////////////////
// isdefined v1.0
// 
// Check if a javascript variable has been defined.
// 
// Author : Jehiah Czebotar
// Website: http://www.jehiah.com
// Usage  : alert(isdefined('myvar'));
// ///////////////////////////
*/
function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}
</script>
Subscribe via RSS ı Email
Jehiah Czebotar