Console log not printing variable from function

▼魔方 西西 提交于 2019-12-06 06:43:32

The variable randomWord is out of the scope. You define the variable inside a function, and then call it outside of it.

You should either define the variable out of the function or call it inside of it:

function strt(){
   var randomWord;
   ...
   console.log(randomWord);
   return randomWord;
}//end strt()

Or

var randomWord;
function strt(){
   ...
   return randomWord;
}//end strt()
strt(); // Call the function
console.log(randomWord);

For the latter, consider that randomWord won't have changed when JS executes the console log function; therefore, it will be null. In other words, you must call the function before you log it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!