How to get rid of the undefined message after console.log()
a variable
var example = 5;
console.log(example);
output: 5
undefined
How to get rid of the undefined message after console.log()
a variable
var example = 5;
console.log(example);
output: 5
undefined
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 returnundefined
.
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.