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

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.

Related

Javascript variable undefined outside if else condition [duplicate]

This question already has answers here:
Chrome/Firefox console.log always appends a line saying 'undefined'
(9 answers)
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I have 2 objects coming from props and I am assigning these objects to their variable by if else condition
let var1;
let var2;
if (props.id === 1) {
var1 = props;
console.log(var1); // returns {id:1,learn:"HTML", do:"cooking"}
} else {
var2 = props;
}
console.log(var1); // returns {id:1,learn:"HTML", do:"cooking"} and undefined
When I console.log after condition why does it returns the object and undefined? But when console.log inside if condition only returns the object.
I want that it returns only the object if console.log after the condition
My two objects coming like this
{id:1,learn:"HTML", do:"cooking"}
{id:2,learn:"Css", do:"home-work"}
I have seen resources from StackOverflow
Javascript variable is undefined outside if condition
JavaScript Variable undefined outside of function
This question is said to be duplicate for Chrome/Firefox console.log always appends a line saying 'undefined'
but it's completely different it's not about the browsers console I am getting undefined from my javascript file
Use const instead of let and try to differentiate the logs of next render from the previous render like this
const var1;
const var2;
if (props.id === 1) {
var1 = data;
console.log(var1);
} else {
var2 = data;
}
console.log(var1);
console.log("--------");

In the JavaScript, Why is the different output coming for Two same properties function? [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 2 years ago.
** I have created two functions with different name & same properties and when I was checking surprisingly it's giving me different output. I don't understand what was is the problem exactly. I was expecting same result for both functions. See the below code**
function foo1()
{
return {
bar: "hello"
};
}
function foo2()
{
return
{
bar: "hello"
};
}
console.log("foo1 returns:");
console.log(foo1());
console.log("foo2 returns:");
console.log(foo2());
Below is the Output:
foo1 returns:
Object {bar: "hello"}
foo2 returns:
undefined
It's because you have a new line after your return statement in the second function.
So instead of returning the object with bar, it just returns without a specified value, which returns undefined.

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

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().

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.

JavaScript, Hoisting between 'var' and without 'var' [duplicate]

This question already has answers here:
Why does an undefined variable in Javascript sometimes evaluate to false and sometimes throw an uncaught ReferenceError?
(8 answers)
Closed 7 years ago.
I understand the following function because variable hoisting happens.
foo();
function foo() {
console.log( a ); // undefined
var a = 2;
}
However what I don't understand is the following part.
I got Reference Error, why?
foo()
function foo() {
console.log( a ); // Reference Error
a = 2;
}
--- Edit ---
So far, what I understand from the answers is the second does not make any hoisting and we cannot use any undefined variable.
foo()
function foo() {
// we cannot use any undefined variable, which "a" here
console.log( a );
window.a = 2;
}
For example
var a;
a; //undefined
b; //Reference error
In the first one, a is declared, but undefined, in the second it's also also undeclared (can't be found in foo or in global scope), so throws a ReferenceError.

Categories

Resources