What is undefined?

hi guys the output of this code is like this =
Lemme get 1 scoop of vanilla undefined Lemme get 3 scoops of chocolate undefined
but what is undefined ?
here is the code

function order(scoops, flavor) {
  if (scoops > 1){
    console.log(`Lemme get ${scoops} scoops of ${flavor}`)
  } else (
    console.log(`Lemme get ${scoops} scoop of ${flavor}`)
  )}



console.log(order(1, 'vanilla'));

console.log(order(3, 'chocolate'));

it happens to me alot when i write somehing i get this undefined
if youu wnat to try it =

function order(scoops, flavor) { if (scoops > 1){ console.log(`Lemme get ${scoops} scoops of ${flavor}`) } else ( console.log(`Lemme get ${scoops} scoop of ${flavor}`) )} console.log(order(1, 'vanilla')); console.log(order(3, 'chocolate'));
1 Like

No need to console.log when you are calling the functions.

Just do this:

order(1, 'vanilla');
order(3, 'chocolate');

When you add console.log to those statements, you are asking for what the order function returns, which is nothing, so you get undefined.

1 Like

Every JavaScript function returns something. If the function doesn’t explicitly return a value, it will implicitly return undefined. Your function, order, logs a message to the console, and then implicitly returns undefined to the caller. The caller is the line of code that called your function. The caller in your case is itself a function call to console.log(), so the return value of undefined is also logged to the console. To avoid logging undefined to the console, you can either do as @jameskeezer suggested, and simply call the order function, or you can have the function explicitly return the values that you want logged to the console, like so:

function order(scoops, flavor) { if (scoops > 1){ return `Lemme get ${scoops} scoops of ${flavor}` } else { return `Lemme get ${scoops} scoop of ${flavor}` } } console.log(order(1, 'vanilla')); console.log(order(3, 'chocolate'));

As far as the title of your topic, “What is undefined?” See undefined - JavaScript | MDN

3 Likes

best answer i could get , thanks for the link too , i checked your answer as the solution

3 Likes

thanks, you really solved my problem

2 Likes