You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用JS函数报Uncaught ReferenceError: Finland is not defined错误

报错产生原因

你在调用decribeCountry函数时,传入的Finland和Helsinki是字符串类型的参数,但没有用引号包裹。JavaScript引擎默认会把未被引号包裹的标识符解析为已定义的变量,而你的代码中从来没有声明过名为Finland、Helsinki的变量,因此触发引用不存在变量的ReferenceError异常。

额外说明:你写的函数名decribeCountry存在拼写笔误,正确拼写应为describeCountry,这个问题不会触发报错,但会降低代码可读性,建议一并修正。

修复方案
  • 给所有作为字符串字面量传入的实参加上引号(单引号、双引号、反引号均可),明确告知引擎这是字符串值,而非待引用的变量。
  • 修复后的完整代码如下:
// 修正了函数名的拼写笔误
function describeCountry(country, population, capitalCity){
  const countryId = `${country} has a population of ${population} and its capital city is ${capitalCity}`;
  return countryId;
}

// 字符串参数用引号包裹
const countrySpecs = describeCountry('Finland', 6000000, 'Helsinki');
console.log(countrySpecs);

运行上述代码后,控制台会正常输出预期结果:

Finland has a population of 6000000 and its capital city is Helsinki

内容的提问来源于stack exchange,提问作者eric Igbinosun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 14:33:28