Why is there "undefined" in the console? [duplicate] - javascript

This question already has an answer here:
Why does this JavaScript code print "undefined" on the console?
(1 answer)
Closed 4 years ago.
Question about javascript from a rookie.
I need your help, thank you.

It says undefined because nothing get returned by the function sayHi ; and you are displaying the return of the function.
function sayHi() {
console.log('Hello');
}
console.log(sayHi());
function sayHi2() {
console.log('Hello');
return 'returned value';
}
console.log(sayHi2());

Because you console.log() your console.log().
You only need to call instructor.sayHi() or your sayHi function should return a string that you console.log().

Related

How to get rid of the undefined message on Chrome? [duplicate]

This question already has answers here:
Chrome/Firefox console.log always appends a line saying 'undefined'
(9 answers)
Closed 1 year ago.
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 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.

Does A Function Always Return A Value [duplicate]

This question already has answers here:
Does every Javascript function have to return a value?
(4 answers)
Closed 5 years ago.
I have been reading a bit about undefined and just started wondering that we see undefined when we declare a function in browser console.
Does calling a function always return a value? If we do not explicitly return a value, then by default undefined value is returned from a function.
That is to say, a function will always return a value. Always. Is that correct?
var aFunc = function(){
console.log( "aFunc ran." );
}
aFunc() === undefined // true
Yes it always returns a value, if explicit one is not given, it returns undefined. This is same as you will write return undefined or just return.

Why does a function stored in an Object return undefined in console at the end of running? [duplicate]

This question already has answers here:
Why does console.log say undefined, and then the correct value? [duplicate]
(5 answers)
Closed 7 years ago.
Was playing around with creating 'class objects'? if that is the correct term.
var cat = {
eyes:2,
pur: function() {
console.log("puuuuurrrrr");
}
};
cat.pur();
In Chrome this returns the console.log message and then on the next line undefined. Was wondering whats causing the undefined to pop up at the end. It doesn't do it when i call cat.eyes. In internet explorer this happens before the console.log() event.In nodeJS this happens after console.log.
you see first in the console what you wrote to the console which is puuuuurrrrr
the undefined is the output of pur() which is nothing meaning undefined.
if you would change pur to
console.log('whatever....');
return 'something';
than you will see instead of 'undefined' the value 'something'.
hope that explains it.

Javascript unexpected return value with specific indentation [duplicate]

This question already has answers here:
Javascript function fails to return object when there is a line-break between the return statement and the object?
(2 answers)
Closed 7 years ago.
I got an unexpected return value "undefined" from function 'fnExample1' if I indent code like this :
function fnExample1() {
return
1;
}
I have made this simple example : Example jsfiddle
Thanks.
write return and 1 in one line
function fnExample1() {
return 1;
}
The reason why your code returns undefined is javascript's "semicolon insertion", where a semicolon is inserted after return, and 1 is considered another statement (which is not executed)

Javascript pointer to function [duplicate]

This question already has an answer here:
Illegal Invocation error when console.log passed in a function
(1 answer)
Closed 8 years ago.
Javascript is def not my main programming language I for practice I wanted to do something simple, at least i thought ;)
This is my code:
function Log () {}
Log.format = function (func, facility, text) {
func("hellow");
};
Log.debug = function(facility, text) {
Log.format(console.warn);
};
When I call Log.debug("we", "deb"); I get a Uncaught TypeError: Illegal invocation error. What am I doing wrong?
Depending on the browser, console methods can only be called in the context of console. Try Log.format(console.warn.bind(console));

Categories

Resources