Printing object property also prints undefined? [duplicate] - javascript

This question already has answers here:
Chrome/Firefox console.log always appends a line saying 'undefined'
(9 answers)
Closed 7 years ago.
I have a JavaScript object:
function Thing() {
this.number = 4;
}
I create an instance and assign a new property:
var myThing = new Thing();
myThing.newProperty = 5;
console.log(myThing.newProperty);
and the output is:
5
undefined
Why is the output also printing undefined?

You don't need to type console.log() into the console. If you type in a variable, it'll print its value.
When you do console.log(myThing.newProperty); in the console, it runs it and shows you its return value.
5 is shown because you ran console.log. undefined is shown because that's console.log's return value.

Related

Why the alert is showing undefined after fred flinston? and what properties does "const" in ES6 have? [duplicate]

This question already has answers here:
console.log(myFunction()) returns undefined
(2 answers)
Closed 2 years ago.
let a = 'Fred Flinstone'; // This is a global variable
function alpha() {
alert(a);
}
alert(alpha());
why the following code displaying undefined after displaying fred flinston?
and what properties does "const" in ES6 have and what is meant by " referencing to memory " in const?
Thanks
Actually, after you use alert(alpha()),this code will do 2 steps:
1.get the return value from alpha()
2.execute the alert function
Because there is no return code from the function alpha , so it return 'undefined' and which will be print in the alert box

Member undefined when calling function indirectly [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 2 years ago.
class Hello{
constructor(member) {
this.member = member;
this.name_function_map = {"print_member" : this.print_member};
}
print_member(){
console.log(this.member);
}
}
let h = new Hello(2);
h.print_member();
//=> works as expected
h.name_function_map["print_member"]();
//=> why is it h. member undefined
the output is:
2
undefined
can someone please enlighten me, why is calling the member function of Hello through a reference stored in a map any different than calling it directly?
And how may i work around that.
When you execute:
let h = new Hello(2);
h.print_member();
The this keyword inside the print_member method will be equal to h (the object instance). That is correct. But, when you execute:
h.name_function_map["print_member"]();
or equally
h.name_function_map.print_member();
the this keyword inside the print_member method will be equal to h.name_function_map, which is:
{"print_member" : this.print_member}
Obviously, this object does not have a member property, and that's why you are getting undefined.

console.log prints undefined [duplicate]

This question already has answers here:
Chrome: console.log, console.debug are not working
(23 answers)
Closed 1 year ago.
When I try to print a simple string with console.log(), it does not print the string out but rather it returns undefined:
> console.log("Hello")
< undefined
What is wrong?
Check your console log levels configuration. Chances are you unchecked info :
because when you write in console it will print the return value of the function, when the function you called have no explicit return javascript implicitly call return undefined.
so when you write console.log("hello") it will return undefined so console print undefined for you
just place your javascript.min.js file within the body tag of html and test

Print undefined in console with no reason, Javascript [duplicate]

This question already has answers here:
Chrome/Firefox console.log always appends a line saying 'undefined'
(9 answers)
Closed 5 years ago.
I have the below block of code which in the console print "undefine" twice after "Hello"
JavaScript Code
function f()
{
function g() {
console.log("Hello");
}
return g;
}
var x = f()();
console.log(x);
While I am trying to print only "Hello", where is the two undefined printing from
Output in console
Hello
undefined
undefined
The first undefined is because f()() evaluates to whatever g returns, and g doesn't return anything. You store that undefined in x, then print it when you write console.log(x);.
I'm guessing the second undefined is the result of running this in a console. The last line containing console.log evaluates to undefined, which may be implicitly printed from the console/REPL. You can confirm this by adding something like the string "World" as the last line of the script. Adding anything else to the last line should get rid of the second undefined.

logging javascript object properties [duplicate]

This question already has answers here:
console.log() async or sync?
(3 answers)
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 5 years ago.
Please share your thoughts regarding below outputs.
var a = '';
console.log(a); //this logs nothing as expected.
a = 'triven';
var a = {};
console.log(a); // How javascript is aware of property here that is defined below.
a.name = 'triven';
I think you're looking for console.dir().
console.log() doesn't do what you want because it prints a reference to the object, and by the time you pop it open, it's changed. console.dir prints a directory of the properties in the object at the time you call it.
The JSON idea below is a good one; you could even go on to parse the JSON string and get a browsable object like what .dir() would give you:
console.log(JSON.parse(JSON.stringify(obj)));

Categories