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

  • Home
  • SEARCH
  • 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 8396043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:26:40+00:00 2026-06-09T20:26:40+00:00

Generally, I test whether or not a variable is set with something like this:

  • 0

Generally, I test whether or not a variable is set with something like this:

if (variable !== '') {

   do something...

}

I know there are other methods for testing variables like typeof but I don’t see any advantage – is this an appropriate way to test whether or not a variable is set? Are there problems with it that I should be aware of ?

  • 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-06-09T20:26:41+00:00Added an answer on June 9, 2026 at 8:26 pm

    Two reasons:

    1) What if the variable is set by getting the contents of an empty input box?

    if(someScenario){
        var variable = $('empty-box').val(); }
    

    Perhaps this is only done in certain cases, like when someScenario is true. Later on, you want to check if that variable was set. Your means returns false rather than true. Point is, you can come up with scenarios where you get wrong answers.

    There’s just no reason not to do it the accepted way.

    if(typeof variable !== 'undefined')
    

    It’s no slower, has no real flaws, and is only a few characters more.

    2) And most importantly, using typeof makes it totally clear what you’re asking. Readability is crucial, and if another programmer read the first code, they would think you were checking that it wasn’t an empty string. The method using typeof makes it perfectly clear what your conditional is looking for, and reduces the odds of mistakes later on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Generally, message boxes work like this: if(ShowMessageBox(Title,Text,MB_YES_NO) == MB_YES) { //the user responded yes
Given a controller like this where it creates several instance variables for use by
Is there any way in which you can test whether an instance of a
I am trying to pass an extra variable, that determines whether or not the
In Python I need to efficiently and generically test whether an attribute of a
I have an app that uses EF. To test my stuff I generally wrap
Generally we import a tlb file at the starting of the program like #include
Generally in programming, the floating point data types should not be compared for equality
Generally speaking, I am very happy with the changes in Xcode 3.2. However, there
Generally speaking, what are your recommendations on this? Currently takes close to 10 minutes

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.