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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:26:28+00:00 2026-06-11T18:26:28+00:00

In javascript I have an option for log output that allows an element/selector to

  • 0

In javascript I have an option for log output that allows an element/selector to be used to specify where to output log/error messages. The output is formatted upon initialization like so:

var $messageOutput = options.messageOutputElement ? $(options.messageOutputElement) : null;

and it is used later on through a log function:

function outputMessage(msg)
{
    if ($messageOutput !== null)
    {
        messageNum++;
        var $messageOutput = $(options.messageOutputElement);
        var html = $messageOutput.html();
        html += '<b>' + messageNum + '</b><br/>' + msg + '<br/>';
        $messageOutput.html(html);
        $messageOutput.scrollTop($messageOutput[0].scrollHeight);
    }
}

The problem is that even when $messageOutput === null, the if statement inside outputMessage() falls through. I’ve verified with Chome’s debugger that $messageOutput indeed equals null. And indeed it steps into the if statement anyway.

Can anyone give me any insight as to why this might be happening? Thanks in advance for any input on this matter.

  • 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-11T18:26:29+00:00Added an answer on June 11, 2026 at 6:26 pm

    By declaring “var $messageOutput” in the scope of a function, you stop referencing the variable in the global scope. So, instead of referring to that, it refers to a variable that is yet to be set within the function body, which is undefined, not null. You should probably remove the “var” part of “var $messageOutput = $(options.messageOutputElement);”, which will instead make the function refer to the $messageOutput outside the function.

    To elaborate a bit:
    “var” creates a new variable within the scope you are in, and to add to the confusion something like “var foo = function” behaves differently than “function foo”. Here are a few fun cases & explanations for your reading pleasure:

    var x = 5;
    
    function f () {
      console.log(x):
      if (!x) {
       var x = 22;
      }
      return x;
    }
    function g () {
      console.log(x):
      if (!x) {
        x = 22;
      }
      return x;
    }
    f(); // prints nothing, and returns 22
    g(); // prints 5, and returns the same value
    

    What you also sometimes have to watch out for is that you can reference a function before declaration outside of the global scope, depending on how it’s declared. For example:

    function f () {
      console.log(f_sub);
      function f_sub () {
        return "subfunction of f";
      }
      return f_sub;
    }
    function g() {
      console.log(g_sub);
      var g_sub = function () {
        return "subfunction of g";
      }
      return g_sub;
    }
    f(); // prints and returns f_sub
    g(); // prints undefined and returns g_sub
    

    A conventional function declaration can be made anywhere and referenced anywhere, but assigning a function to a variable means it can’t be referenced until the line has been executed.

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

Sidebar

Related Questions

I have a javascript that returns a value whenever I choose an option from
We have a jquery/php/mysql system that allows a user to log in and review
I am using some pre-written JavaScript from polldaddy. They have a JavaScript option which,
I have some javascript where I append an option to a select: var myuniqueddl
Does jquery/javascript have any event model that you can attach to your objects? Basically
var object = {foo: 'bar'}; Does JavaScript have a way to determine that object.foo
Javascript I have code that will hide various sections in a MS CRM form
I have an javascript object that contains a select list. I'd like to loop
I have a PHP function that returns an array. This is the output of
Imagine we have a server side application that generates streaming content full of JavaScript

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.