0

Why is it that I have to define a javascript string variable before using the concat() method on it? In the code below the console prints nothing. But if I initialize the variable string like this var string = ""; it concatenates like it should and prints the string in the console. This confuses me because I thought that javascript has automatic type casting, so var string; should initialize the variable, then when concat() is used on it, it should automatically change the type to "String" and concatenate?

var select = document.getElementById("CK_Expertise");
options = select.getElementsByTagName('option');
var string;

for (var i=options.length; i--;){
    if(options[i].selected){
        string = string.concat(options[i].value + "<br/>"); 
        }
}
console.log(string);

Thank you - Chris

6
  • how does one type cast nothing into string? "" is not nothing, its an empty character array Commented Mar 7, 2014 at 23:09
  • The type coercion can only happen when you assign something. For the .concat function to even work, the js engine will temporarily create a String object behind the scenes, but it needs to know that it's working with a string type first. Commented Mar 7, 2014 at 23:10
  • 1
    "Why is it that I have to define a java string variable..." Java or JavaScript? They're completely different languages. Commented Mar 7, 2014 at 23:10
  • 2
    As an aside, note that .concat() is not generally recommended for use on strings: you can and should concatenate with the + or += operators. Commented Mar 7, 2014 at 23:13
  • 1
    .concat() is quite a lot slower than +. Also because it is not commonly used it makes code that does use it a bit harder to read: most JS programmers would expect to see + or +=. Commented Mar 7, 2014 at 23:36

2 Answers 2

7

This confuses me because I thought that javascript has automatic type casting, so var string; should initialize the variable, then when concat() is used on it, it should automatically change the type to "String" and concatenate?

var string declares the variable. It starts out with the value undefined (which is not a string). undefined doesn't have a method called concat, and so string.concat(...) fails.

Whereas var string = "" declares the variable and gives it a string value, "". Since that's a string, it has a concat method, and so string.concat(...) works.

Sign up to request clarification or add additional context in comments.

2 Comments

I understand now. since in the code snippet I posted I am trying to use the concat() method associated with the String type, and string has no value and undefined type, the method concat() is not called so the string wont be assigned to String type because concat() wont be called. (basically re-iterating what you said). Thank you T.J.
@kiwicomb123: You're welcome! One small thing, though; you said "...and string has no value and undefined type..." It does have a value; the value is undefined. (Yes, that does seem a bit strange, but it's true. undefined is a value just like "" and false and 27 are values.) :-)
0

Unless a variable has a value, it has no type, and no mehtods. There is automatic typecasting in cases like 5 + "5" -> "55", but not in your's.

10 Comments

So even if a variable is assigned a value after it is declared it doesn't change the type of that variable?
"Unless a variable has a value, it has no type" - A variable will always have some value, but that value might be undefined.
@nnnnnn It depends on one's definition of "type", and "value".
@sabof: undefined is a value, and it has a type.
|

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.