1

How to get rid of the undefined message after console.log() a variable

var example = 5;
console.log(example);

output: 5
undefined

It'sNotMe
  • 1,184
  • 1
  • 9
  • 29
J.Denver
  • 11
  • 3

1 Answers1

0

You can't:

The reason the debug console outputs undefined after a function, is because you're running the function (example()) and then in the function body, there is no return statement so by default, the return statement will return undefined.

More about return statements at MDN

The reason it does, is: say you have a function called example

function example() {
  // Code here
}

Using the return statement inside the function, will return something:

function example() {
  // Code here
  return "Some string";
}

By default (with no return statement) there will be an undefined return statement.

function example() {
  // Code here
}

example(); // undefined
function example() {
  // Code here
  return true;
}

example(); // true

Also, it does not happen only on Chrome. It happens on all modern and old browsers with JavaScript enabled.

Parking Master
  • 551
  • 1
  • 4
  • 20