Simple js FOR loop returning 'undefined'
问题 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); } 回答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