0

When making an unordered list from JS by using Array, first value is undefined.

var list; //box for accumulating list tags
var arr = ['menu1', 'menu2', 'menu3', 'menu4'];

for(var i = 0; i < arr.length; i++) {
    list += '<li>' + arr[i] + '</li>';
}

document.getElementById('menu').innerHTML = list;

result:

<ul id="menu">
undefined
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
<li>menu4</li>
</ul>

But assign null value at first, it will be fine.

var list = "";

So I want to know what the difference is.

If this question is redundant, I'll delete it soon.

Thank you for stopping by.

5
  • 1
    Because initial value of variable (unless specified otherwise) is a special value undefined. When converted to String, it's 'undefined'. Commented Aug 20, 2014 at 7:08
  • list='<li>'+arr.join('</li><li>')+'</li>' Commented Aug 20, 2014 at 7:09
  • thanx raina! That says it all. I'll be cautious when making variable from next time. Commented Aug 20, 2014 at 7:09
  • 2
    The important thing to note here is that the + operator coerces everything to strings if its arguments are incompatible, and undefined is stringified to "undefined" rather than "" Commented Aug 20, 2014 at 7:17
  • Another thing I did not know that change of data type happens as well.I'll be aware of that too. @Pumbaa, Thanks. Commented Aug 20, 2014 at 7:21

6 Answers 6

1

No its not, the error lies here

list += 

That assumes list had some value before and it doesn't have it on the first run and is undefined, that's why the first line is undefined. You should add a check there to assign rather than to concatenate on the first iteration.

Instead of initializing the list with an empty string as being suggested elsewhere (there's no point in concatenating something with an empty string), you should really be doing

for(var i = 0; i < arr.length; i++) {
    if(i==0)
       list = '<li>' + arr[i] + '</li>';
    else
      list += '<li>' + arr[i] + '</li>';
}
Sign up to request clarification or add additional context in comments.

Comments

1

In JavaScript values are undefined until they are initialized.

Let's break down the cases by example to help make this clear.

Referencing an Undeclared Variable

In JavaScript you must declare a variable before you may reference it. For instance,

var x;
console.log(x);

If you were to just type the following a reference error would occur.

console.log(x);

Referencing an Uninitialized Variable

Once you declare a variable, using the var keyword, it can be used in any expression, but has an initial value of undefined. For instance

var x;
x === undefined; // true

Give a Variable Type, by Giving it a Value

Once you give a variable a value, or give it any new value, you essentially let the interpreter know both the value and the type of the variable.

For instance,

var x;
typeof x; // undefined
x = 1;
typeof x; // number
x = 'a';
typeof x // string

Mixed Type Expressions

In JavaScript, if an expression is of mixed type the the interpreter will coerce one of the values to be the type of the other.

For instance,

var x = 1;
var y = 'a';
z = x + y; // '1a'
typeof z // string

The type coercion is performed on all expressions, not just +, and is not always straight forward. For instance,

[] == false // true

Comments

0

Problem Part

That is because when you define a variable & don't initialize, it will be undefined.

var list; //list is undefined here

So when you are first time saying

list += arr[i]

So it is basically

list = undefined + arr[0];

So last output will be

'undefined<li>...'

Working Part

You are saying

var list = '';

So first time it will be

list = '' + arr[0];

So last output will be

'<li>...'

Comments

0

assign empty string when you declare list variable

when you use list+= means you have already value in list variable but you have not

var list=""; //box for accumulating list tags
var arr = ['menu1', 'menu2', 'menu3', 'menu4'];

for(var i = 0; i < arr.length; i++) {
  // here is `list` is undefined when loop execute first time 
    list += '<li>' + arr[i] + '</li>';
}

document.getElementById('menu').innerHTML = list;

Comments

0

JS is a dynamic language so if you just define a var there is no way to know which type is will be. It is another case if you use var list = ""; You explicitly tell that the type will be string and the concatenation will run in the desired way!

Comments

0

This is because when you define a variable like so:

var list;

It's undefined. To fix this simply make it an empty string, as you have said.

var list = "";

As a few answers have already mentioned. When you concatenate a undefined variable with a string, undefined turns into the string 'undefined' + ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.