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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:50:00+00:00 2026-06-01T05:50:00+00:00

I have many js methods that uses global variables like: function DoSomething() { var

  • 0

I have many js methods that uses global variables like:

function DoSomething() {
    var val=$('#'+clientId).val();
    .
    .
    .
}

DoSomething uses clientId variable. The problem is that clientId is not always declared and therefore I get an exception of undeclared variable.
I have many places like this and add if (typeof clientId == "undefined") check everywhere is a lot of work and code duplication (duplication of the check). Is there I can workaround it? for example use a method that returns null if the variable is not declared? (In such way I can control on the return value in case of an undefined for example)

  • 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-01T05:50:01+00:00Added an answer on June 1, 2026 at 5:50 am

    Just an idea: all variables are properties of a namespace, up to the global namespace (in a browser, that’s window). A property that’s not assigned evaluates to undefined. So if you want to know if a variable is assigned, you can use if (property in namespace). If it concerns a global variable in a browser: if (clientId in window). Now you can make a function out of that:

    function exists(label, namespace){
      namespace = namespace || window;
      return namespace[label] || false;
    }
    // possible usage
    function DoSomething() {
     var val=$('#'+( exists('clientId')||0) ).val();
     // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF service with many methods. I would like that after executing
I have a toolkit that has many methods often taking Expression<Func<T,TProperty>> as parameters. Some
Occasionally , we have to write methods that receive many many arguments , for
I have a form that has many long-methods. My quest is: What is the
I have an ActiveX/COM DLL. It contains many methods and properties. I would like
I have a fairly generic C# socket server that uses the asynchronous methods of
I have a Rails project that uses bundler. Many of the gems I'm included
I have a large web project that uses log4j directly, together with many 3rd-party
Say I have a class with synchronized methods. It's actually a class that uses
I have an application that uses several different Java classes and would like to

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.