Skip to navigation

Javascript is_null()

Click here to view the is_null() demo

There isn't an is_null() function in JavaScript as there is in PHP. Here is a simple is_null() function that you can copy and paste into your JavaScript:

The Function

<script type="text/javascript">
  function is_null(input){
    return input==null;
  }
</script>

Using is_null

You can use is_null() right in your JavaScript code, just like you would use it in PHP. In this example, we figure out if the variable age is a null:

<script type="text/javascript">
  //is age null?
  if(is_null(age)){
    alert("Your age is null...");
  } else {
    alert("Your age is not null!");
  }
</script>

Code Explanation

Check out this one-liner JavaScript function!

The null variable in JavaScript is the same as in PHP. This function just returns true if the input equals null.