in this case, the name of the parameter coincides with the name of the global variable and overlaps it inside the function.
Not to be confused with this, you can rename the parameter and get the following
var someWords = ["clear", "business", "entertainment"]; function pickWord(words) { return words[Math.floor(Math.random() * words.length)]; }
Thus, the value of the someWords parameter is determined by which parameter the pickWord function was called pickWord .
If the function was called without parameters: pickWord() then the value of all declared parameters of the function will be undefined .
To solve, you must either pass a global variable to the function
var someWords = ["clear", "business", "entertainment"]; function pickWord(words) { return words[Math.floor(Math.random() * words.length)]; } pickWord(someWords);
Or remove the parameter
var someWords = ["clear", "business", "entertainment"]; function pickWord() { return someWords[Math.floor(Math.random() * someWords.length)]; } pickWord();
In this case, the global variable will be used.
pickWord()- Grundy