Can't invoke my function

I’m trying to test this function but when I invoke it I don’t get the result I’m looking for:

const JokeBase = () => {
const JokeBases = [‘The past, present, and future’, ‘The NSA’, ‘Comic Sans, Helvetica, and Times New Roman’, ‘An amnesiac’,
‘A neutron’];
return JokeBases[Math.floor(Math.random() * 4)]
}; console.log(JokeBase);

JokeBase();

The result form the terminal is [Function: JokeBase]

How do I get the function to perform properly?

To preserve code formatting in forum posts, see: How do I format code in my posts?

// You wrote:

const JokeBase = () => {
// ... body of function
}

console.log(JokeBase);
JokeBase();

JokeBase is the name of your function.

When you write the name of the function such as in the statement

console.log(JokeBase);

it doesn’t execute the function. It just prints that the JokeBase variable is pointing towards a function.
When you write the statement

JokeBase();

it executes the function (note the parentheses after the function name) and returns a value. But you aren’t doing anything with the returned value, so it just disappears without anything useful being shown.

Instead, you can print the returned value by doing:

// Instead of
console.log(JokeBase);
JokeBase();

// You can do
console.log(JokeBase());

// OR

// If you want to save the returned value, you 
// can assign it to a variable before printing.
let result = JokeBase();
console.log(result);
1 Like

thanks so much! this saved my afternoon
S

1 Like