我遇到了返回undefined的JavaScript问题(这可能更多是由于我对JS的理解)。
我有两个类似的功能:
getData = function(name) {
var string = "You're name is " + name;
alert("created string is: " + string); // this alerts "You're name is John"
return string;
}
firstFunction = function() {
newString = getData("John");
alert(newString); // this allerts "undefined"
}
firstFunction();
getData将undefined返回到firstFunction的原因是什么?
答案 0 :(得分:0)
<script>
const getData = name => {
var string = `You're name is ${name}`;
alert(`created string is: ${string}`); // this alerts "You're name is John"
// The return string command is not required
}
const firstFunction = () => {
getData("John");
}
firstFunction();
</script>