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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:12:36+00:00 2026-06-11T10:12:36+00:00

Why does this piece of code return – null – when it should return

  • 0

Why does this piece of code return - null - when it should return - - by my understanding – it seems to be treating null like a string.

var testvar = null;
alert(" - "+testvar+" - ");

Thats it. The same goes for undefined. I need this to work as I have an array, and I loop to loop through the array, and add each item to a variable, which is a string.

I have this:

//'resp' variable is a JSON response, decoded with JSON.parse.  This part works fine.
var addOnEnd=null;
for (item in resp) {
    console.log(">"+item);
    addOnEnd += item+"\n";
}

The console.log reads what I’d expect – a list of all the items in the response.

However, if I alert(addOnEnd) after the for loop, it returns ‘undefined’ (literally the string) and then the rest of the array as it should.

What am I doing wrong?

  • 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-11T10:12:37+00:00Added an answer on June 11, 2026 at 10:12 am

    Change it to this:

    alert(" - " + (testvar || "") + " - ");

    …and this…

    addOnEnd += (item || "") + "\n";

    You will also need to initialize addOnEnd as an empty string instead of null.

    This way, if the value is undefined (which when evaluated as a boolean returns false) it’ll use the ‘default’ value of an empty string.

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

Sidebar

Related Questions

Try this piece of code. Why does getValueB() return 1 instead of 2? After
Why does this particular piece of code return false on the strstr() if I
my text editor does not like this piece of code I am not sure
I found this piece of code which seems pretty cool. I see return new
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
This piece of code used to work in MVC 1 but it does not
Does this look like it should work? I'm wanting to generate directions from one
I have this piece of code: $(document).on(click, #breadNavMain, function() { for(var i = 0;
char *myfunc() { char *temp = string; return temp; } In this piece of
I've got this piece of code: using Posix; int fuseguifs_getattr(string path, Posix.Stat *stbuf) {

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.