最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Simple js FOR loop returning 'undefined' - Stack Overflow

programmeradmin0浏览0评论

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);
}

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);
}
Share Improve this question edited Feb 23, 2015 at 0:36 thefourtheye 239k53 gold badges465 silver badges500 bronze badges asked Feb 23, 2015 at 0:22 Uncle SlugUncle Slug 9132 gold badges14 silver badges26 bronze badges 1
  • 6 var newStr = ''; undefined + 'qwe' === 'undefinedqwe' – zerkms Commented Feb 23, 2015 at 0:23
Add a comment  | 

4 Answers 4

Reset to default 21
  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 it

    var newStr;
    console.log(newStr);
    // undefined
    console.log(newStr + "thefourtheye");
    // undefinedthefourtheye
    

    So, just initialize the variable with an empty string, like this

    var newStr = '';
    
  2. Also, note that, in this line

    for(i=0; i < x.length; i++) {
    

    i has never been declared before. So, a new global variable i will be created. You may not want that. So, just use var keyword to declare the variable scoped to the current function, like this

    for (var i = 0; i < x.length; i++) {
    
  3. 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, returns undefined. That is why you are getting the second undefined in the question. To fix that, use return statement like this

    function translate2(x) {
        var newStr = "";
        for (var i = 0; i < x.length; i++) {
            newStr += x.charAt(i);
        }
        return newStr;
    }
    

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.

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);
}

The above answers are not correct. The console.log() will run before the loop finishes and that is why you get undefiend. You can find your answer here.

you have to think sync like this piece of code:

function delay() {
  return new Promise(resolve => setTimeout(resolve, 300));
}

async function delayedLog(item) {
  // notice that we can await a function
  // that returns a promise
  await delay();
  console.log(item);
}
async function processArray(array) {
  for (const item of array) {
    await delayedLog(item);
  }
  console.log('Done!');
}
processArray([1, 2, 3]);

this will give you 1,2,3,done which means the console.log is happening at the end of loop!

发布评论

评论列表(0)

  1. 暂无评论