Why does this JavaScript code print undefined on the console?
It's because the "printCounter()" function itself returns undefined
. That's the console telling you the result of the expression.
Change "printCounter()" by adding return "Hello Anton!";
to the end :-)
It's a little confusing to say it "returns undefined
"; really, it has no explicit return, but it's the same effect.
Why does it print undefined on the console?
I suppose you're calling console.log(kim.message());
and console.log(kim.interest());
console.log prints the return value of the passed function but because both the function don't have a return statement console.log prints undefined
that's way you see those additional undefined
logs.
Why this piece of code prints `undefined` at the end?
The undefined
is simply the console's way of telling you that the statement:
console.log(solution.toString());
doesn't, itself, return a value (it outputs a value, but doesn't return one). It's not something to worry about when your actual code executes because it's a specific behavior to the developer's console.
As another example, if you type: 5 + 6
into the console, it will report 11
on the next console line, because the console always wants to report any value returned from the statement it just executed.
Why does the console print undefined when I assign a variable?
TL;DR: It does not.
You can see content of your variable test
, il will output the same thing as before. In fact it is the variable assignement that returns the undefined
you see here.
For instance:
var test = 'Hello' // => undefined
test // => 'Hello'
Another case is printing your variable with console.log
. If you do so, the return value will be undefined
but the output will be your variable content (Hello here).
console.log(test) // return: undefined / print: Hello
Why the console.log prints to the console undefined when I try to add some results in the function?
You're not returning anything in your getActualSleepHours
function. Get rid of the braces so it goes from
const getActualSleepHours = () => {
getSleepHours("monday") +
getSleepHours("tuesday") +
getSleepHours("wednesday") +
getSleepHours("thursday") +
getSleepHours("friday") +
getSleepHours("saturday") +
getSleepHours("sunday");
};
to
const getActualSleepHours = () =>
getSleepHours("monday") +
getSleepHours("tuesday") +
getSleepHours("wednesday") +
getSleepHours("thursday") +
getSleepHours("friday") +
getSleepHours("saturday") +
getSleepHours("sunday");
Related Topics
Ajax, Back Button and Dom Updates
Convert String in Dot Notation to Get the Object Reference
What Does $(Function() {} ); Do
JavaScript Associative Array to JSON
How to Convert an Integer to Binary in JavaScript
Check for Special Characters in String
How to Escape JavaScript in Jsp
How to Watch for a Route Change in Angularjs
How to Get the (X, Y) Pixel Coordinates of the Caret in Text Boxes
What Do Curly Braces Inside of Function Parameter Lists Do in Es6
How to Create a Function from a String with JavaScript
Scrolling an Iframe with JavaScript
What's the Best Way to Break from Nested Loops in JavaScript
Three.Js Generate Uv Coordinate
How to Replace Remapcolums with Remapcolumnsbyname in Free Jqgrid
Precise Financial Calculation in JavaScript. What Are the Gotchas