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 8350801
In Process

The Archive Base Latest Questions

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

I am trying to create a reusable function that checks if a variable is

  • 0

I am trying to create a reusable function that checks if a variable is undefined or not. The strange thing is that it does not work when I pass the variable to the function to execute the code, but if I use the same logic outside of the function, it works. Is there any way to get this function isDefined to work?

//THIS WORKS AND RETURN FALSE
alert(typeof sdfsdfsdfsdf !== 'undefined');

//THIS GIVES AN ERROR, WHY?
//Uncaught ReferenceError: sdfsd is not defined 
function isDefined(value) {
        alert(typeof value !== 'undefined' && value !== null)
}

isDefined(sdfsd);

​Live example here (check the console for errors): http://jsfiddle.net/JzJHc/

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

    You cannot use a variable that hasn’t been declared unless it’s in a typeof test

    When you try to pass a variable that hasn’t been declared into a function, that is considered using that undeclared variable. You’ll notice that the error is in the caller, not inside isDefined

    You need to run the check for

    if (typeof sdsdsd !== 'undefined')
    

    before you pass it into the function. Basically that means you can’t write a isDefined function that accepts undeclared variables. Your function can only work for undefined properties (which are OK to pass around)

    However, I am curious, what is the real world case where you’re passing a variable that doesn’t exist? You should declare all your variables and they should exist already. Had you declared var sdsdsds it would exist with the value of undefined and your isDefined function would work just fine.

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

Sidebar

Related Questions

I am trying to create a reusable function that can open a single instance
I'm trying to create a view that only houses reusable HTML blocks that can
I'm trying to create a reusable UserControl in WPF that has a Label and
I'm trying to create a generic, reusable view, that looks like a lined notepad.
What I am trying to do is create a bit of reusable code that
Background / Problem I'm trying to create a reusable JSF composite component that can
I'm trying to create a reusable component to display some photo collection. The basic
I am trying to create some reusable components in Silverlight2 . The difficulty comes
I am trying to create a Validation in a reusable fashion. Purpose: Make the
Trying to create a facebook app just to learn and coming across a strange

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.