Date object with variable says undefined [duplicate] - javascript

This question already has an answer here:
Console returns undefined [duplicate]
(1 answer)
Closed 8 years ago.
Why I am getting undefined for this below statement in console?
var someDate=new Date(1337986800000);
But with out assigning to a variable it works fine
new Date(1337986800000);
Why is it so?

Just type this:
var someDate=new Date(1337986800000); someDate;
It is how the console works.
When you are doing just new Date(1337986800000);, the constructor is returning the object which is printed in the screen.
but when you assign it to a variable, the variable holds the return value, so the console has nothing to do but print undefined. So you'll need to explicitly call the variable to get the output which you are expecting

What you do is create a new Date instance. In the first example you store the instance in a variable; you get undefined because the constructor function itself doesn't explicitly return anything.
In the second example you ask the console to evaluate an expression, which is calling the date constructor, so it just returns you the resulting instance.

Related

In Javascript, how does the window object see property values before the property is assigned a value? [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Weird behavior with objects & console.log [duplicate]
(2 answers)
Closed 1 year ago.
From my understanding, all properties created are given the value of undefined after the execution phase of the Execution Context - then, during the creation phase, the JS engine goes through the script, line by line, and assigns a value if it finds an = operator. With this in mind:
console.log(window);
var myVar = 1;
From the above snippet, why does the myVar property, within the global object, show a value of 1? I would have thought it would show a value of undefined, as this is what it was set to during the execution phase? If I try to access the property directly, like:
console.log(window.myVar);
var myVar = 1;
I DO see the value as undefined... it's only when I log the entire global object I'm seeing the value as 1. Am I missing something here?
Note - this is simply for learning purposes.
this has nothing to do with the window object, this is called hoisting
Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.
another example would be
function test(){
console.log(myVar);
var myVar = 1;
}
test()
this would still log myVar as undefined. while omitting the myVar would throw an Exception..
Edit:
this only happens in the chrome console, it would be undefined otherwise.

Why does Javascript cast null to string at variable assignment? [duplicate]

This question already has answers here:
Using the variable "name" doesn't work with a JS object
(4 answers)
Closed 6 years ago.
In Firefox 45 on OSX, when I fetch an item from localStorage from a key that does not exist, the function call returns null. I tested this in the console.
If I instead assign the call result to a variable, and print its value in the console, I get "null", i.e. a string.
Why does a variable assignment of a previously not defined variable cast a call result to a String?
Used code (in the console):
localStorage.getItem("non-existing-key"); // returns null
var x = localStorage.getItem("non-existing-key");
x // returns "null"
Edit: both versions seem to behave correctly on Chrome 50.0.2661.86 on OSX (both return null)
Edit2: my mistake. I used another variable name in my tests (specifically: var name). Now, if I let the console return the value of the variable name, it returns window.name, which is a property of window of the type String, defaulting to "null". So, it's not an assignment that causes a cast, but instead its that I got a String property defined by window.
I made a mistake. The specific code I used was the following:
var name = localStorage.getItem("non-existing-key");
name
Now, getItem does return null and not a String. What then happens is that by letting the console print the value of name it does in fact get window.name (see window.name on MDN), which by default is "null" (a String).

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.

Undefined yet defined variable? [duplicate]

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.

Passing undefined parameter to function - check if variable exists [duplicate]

This question already has an answer here:
Error when passing undefined variable to function?
(1 answer)
Closed 9 years ago.
Consider the following Javascript:
function getType(obj){
return(typeof(obj))
}
alert(typeof(obj)) //alerts "undefined" correctly
alert(getType(obj)) //throws an error: ReferenceError: obj is not defined
Why might this be happening? Is there any workaround? I am trying to write a function which checks if a variable exists.
The problem is nothing to do with typeof. The problem is that you cant pass undefined variables to functions.
function doNothing(obj){
}
doNothing(obj);
This code too results in the error: Uncaught ReferenceError: obj is not defined
So it doesn't matter what code you write inside your function, as it won't be called. The error happens before the function call.
typeof is an operator, not a function.
This is why it does not behave in the same way as functions.
typeof is an operator, not a function, and therefore has powers that a function can't have. There's no way to do what you're trying to do.
Your function fails as soon as you try to pass an undefined object. You can't encapsulate the typeof() function. Well, you can, but it will always throw errors when passed undefined objects.

Categories

Resources