Simple js FOR loop returning 'undefined'
Solution 1
-
In JavaScript, if a variable is not initialized explicitly, it will by default have
undefined
. That is not a string but a primitive type of the Language. You can check that by printing itvar newStr; console.log(newStr); // undefined console.log(newStr + "thefourtheye"); // undefinedthefourtheye
So, just initialize the variable with an empty string, like this
var newStr = '';
-
Also, note that, in this line
for(i=0; i < x.length; i++) {
i
has never been declared before. So, a new global variablei
will be created. You may not want that. So, just usevar
keyword to declare the variable scoped to the current function, like thisfor (var i = 0; i < x.length; i++) {
-
Apart from that,
translate2
is a function and when it is invoked, one would expect it to return something. But you are not returning anything explicitly. So, again, JavaScript, by default, returnsundefined
. That is why you are getting the secondundefined
in the question. To fix that, usereturn
statement like thisfunction translate2(x) { var newStr = ""; for (var i = 0; i < x.length; i++) { newStr += x.charAt(i); } return newStr; }
Solution 2
You should first initialize the variable newStr.
var newStr = '';
Otherwise, newStr will be undefined and undefined + "asa" = "undefinedasa" in javascript. If you don't know what is undefined, check this out.
Solution 3
newStr is undefined. Add
var newStr = '';
So that you have
function translate2(x){
var newStr='';
x = "Hello World";
for(i=0; i<x.length; i++) {
newStr+=x.charAt(i);
}
console.log(newStr);
}
Uncle Slug
I'm a UI/UX designer. I have always been familiar with the basics of HTML and CSS but I've only actually been coding and programming with JavaScript and jQuery since the middle of 2013. I'll be asking a lot of questions. Thanks in advance.
Updated on February 23, 2020Comments
-
Uncle Slug about 4 years
Not sure what I'm doing wrong here; the variable
newStr
should just return "Hello World", but I'm getting this instead:"undefinedHello World" undefined
JS
function translate2(x){ var newStr; x = "Hello World"; for(i=0; i<x.length; i++) { newStr+=x.charAt(i); } console.log(newStr); }
-
Uncle Slug about 9 yearsGreat, that worked. However, an additional undefined still remains in the console after the string is returned. Why would that be occurring?
-
thefourtheye about 9 years@UncleSlug Did you try the third suggestion in my answer?
-
Uncle Slug about 9 yearsYes, I used the 3rd suggestion.
-
thefourtheye about 9 years@UncleSlug In the console, don't use
console.log(translate2(..))
, try simplytranslate2(..)
. Theconsole.log
function call doesn't return anything, soundefined
will be returned by default. That is why you are getting an extraundefined
in the console. -
Uncle Slug about 9 yearsin the console, I have been using just the function
translate2()
-
thefourtheye about 9 years@UncleSlug Okay, can you please show the current
translate2
function? -
Uncle Slug about 9 years
function translate2(x){ var newStr=''; x = "Hello World"; for(var i=0; i<x.length; i++) { newStr+=x.charAt(i); } console.log(newStr); }
-
thefourtheye about 9 yearsLet us continue this discussion in chat.