Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6766477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:50:08+00:00 2026-05-26T14:50:08+00:00

There are a variety of places where I need to check if a JavaScript

  • 0

There are a variety of places where I need to check if a JavaScript variable is null or empty string so I wrote an extension method that looks like this:

Object.prototype.IsNullOrEmptyString = function()
{
    return (this == null || (typeof this === "string" && this.length == 0));
}

I then call it like so:

var someVariable = null;

if (someVariable.IsNullOrEmptyString())
    alert("do something");

But that doesn’t work because, at the point of evaluation in the if statement, someVariable is null. The error I keep getting is “someVariable is null”.

You can see it live here: http://jsfiddle.net/AhnkF/ (run in Firefox and notice the error console)

Is there anyway to do what I want and have a single extension method check for null and other things at the same time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:50:09+00:00Added an answer on May 26, 2026 at 2:50 pm

    null does not have any properties or methods. You have to create a function, and pass the variable, so it can be tested. Note: To test whether a variable is really an empty string, using === "" is recommended.*

    function isNullOrEmpty(test) {
        return test === null || test === "";
    }
    
    var someVariable = null;
    if (isNullOrEmpty(someVariable)) alert("Do something");
    

    * about == and ===. The following comparisons are true:

    null == undefined
    "" == 0;
    "" == false
    "" == 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It looks like there are a variety of virtual machine offerings available. What is
I'm building the interface within interface builder and see that there's a variety of
There is a moment in my app, that I need to force to show
I've seen that to create division operations there is variety of possibilities, UDiv/SDiv and
I've heard from a variety of places that global variables are inherently nasty and
There is a column that exists in 2 tables. In table 1, this column
There is a variety of Game Engines out on the market, free as commercial.
There is a variety of devices on the market, with different screen sizes, resolutions
Okay, my situation is such: I need to send the contents of a JavaScript
I have an app written in C# that does a lot of string comparison.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.