Undefined yet defined variable? [duplicate] - javascript

This question already has answers here:
Why does JavaScript variable declaration at console results in "undefined" being printed?
(4 answers)
Closed 8 years ago.
Here I define a variable:
var number = Math.round(Math.random() * 10);
When I plug it into the Chrome DevTools JavaScript Console, I get a very weird error: undefined. I have never seen this error before, and I don't see how the variable is undefined.
Is there something I'm doing wrong?
(I'm sure this is one of those in-plain-sight issues.)

That's right. This expression returns undefined - because var doesn't return anything.
But if you type number and press enter you will get the result.

Related

Strange browser js console behavior [duplicate]

This question already has answers here:
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
console.log() async or sync?
(3 answers)
Weird behavior with objects & console.log [duplicate]
(2 answers)
Closed 7 months ago.
Hey mods the other question you linked had nothing to do with mine. Even on that questions page it says that the behavior doesn't exist anymore.
To preface, this isn't an issue with the variable i in the closures all being the same although it might look like it is.
The following code has a weird behavior. Basically the code runs the body of a for loop 3 times where it creates a set timeout that modifies and logs the same array.
const obj = {a:1,b:{c:2}};
console.log(obj);
obj.b.c = 3;
I expected when expanded the log would show that c was 2, but it actually shows that c is 3.
Why does this happen?
(I already answered below)
What is happening is that when you console.log a object only the first level of properties are stored before expanding in the console(clicking the > on an object property and seeing its properties). Think about the alternative, you deep copy every object that is logged. Then because of properties like __proto__ and constructor you would have to copy many many items. And if the object happens to have some reference to window you would practically have to take a memory snapshot.

programmatically creating classes undefined [duplicate]

This question already has answers here:
javascript if giving "undefined" in console [duplicate]
(2 answers)
Closed 3 years ago.
I am following a book on Javascript and the example doesn't seem to return any value. I don't have much experience with OOP though.
I believe this is a programmatical way to create classes, but it returns undefined any way
What do you make of this?
Thank you in advance
This is expected, variable declarations always return undefined.
E.g., let a = 0, var b = 0, const c = 0.
This doesn't mean a, b, and c are undefined though.
Likewise for your example, createClass and Book aren't undefined, as seen by your console.log.

Javascript 'not defined' error when declaring default function parameter matching to a constant? [duplicate]

This question already has an answer here:
Scope of Default function parameters in javascript
(1 answer)
Closed 4 years ago.
Just discovered some weird error that reads:
Uncaught ReferenceError: symbol is not defined
Code in question:
const symbol='tNEOUSD';
function get_position(symbol=symbol){
console.log(symbol);
}
get_position();
How come it's not defined? That's a really weird!
On the other hand if I use a different parameter name it works just fine:
const symbol='tNEOUSD';
function get_position(sym=symbol){
console.log(sym);
}
get_position();
Does anyone know why this happens?
You cant use a variable which is already declared outside as a parameter.

Why initialization of a variable returns undefined in console? [duplicate]

This question already has answers here:
Why does JavaScript variable declaration at console results in "undefined" being printed?
(4 answers)
Closed 5 years ago.
whenever we define a variable in console with some value for example
var f = 20;
var j = 30;
the above statement returns undefined for one time, can you please help in understanding why it returns undefined even it we have defined both the variables?
Secondly if it is related to hoisting then why undefined is coming only once?
The console reports the result of evaluating the expression, equivalent to typeof basically.
typeof eval("var f = 20;");
returns undefined

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.

Categories

Resources