Is this computer broken, or is my head? Why won't this work?


#1

var name;
if(name === undefined )
{console.log("true");}
else{console.log("false");}
even though I have declared name. it is still not defined. So when I run this code you would expect it to return true right? WRONG! Everytime I run it I get false. I even tried

if(typeof(name) === "undefined")
Still i get false. I was going to be very productive today. but nope. i have been stuck on this for the past 30 minutes.


#2

It's because name = ""; , but I can't understand why.
ECMAScript 5.1 (ECMA-262) says:

Variables are initialised to undefined when created. A variable with an Initialiser is assigned the value of its AssignmentExpression when the VariableStatement is executed, not when the variable is created.

var name; // here variable name was declared and should be undefined, but we have ""
if(name === undefined )
   console.log("true");
else
   console.log("false");

ECMAScript 2015 (6th Edition, ECMA-262) says:

Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created. ... A variable defined by a VariableDeclaration with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the VariableDeclaration is executed, not when the variable is created.

If I understood correctly, name should be undefined. But when I pasted your code to browser console and debugged it, I got name = "". That's why you have "false" as a result. "Is this computer broken, or is my head?"

So, I created my own validation:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to test if a variable is undefined.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var name;

    if (name === undefined) {
        txt = "name is undefined";
    } else {
        txt = "name is defined";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Result:
"name is undefined"

Is my computer broken, or is my head?

UPDATED. I can't explain it:

var name;

if(name === undefined) {
   console.log("true");
} else {
   console.log("false");
}
typeof name;
VM332:6 false
"string"

/************/
var name;
typeof name;
if(name === undefined) {
   console.log("true");
} else {
   console.log("false");
}
VM333:6 false
undefined

#3

wow, you went well out of your way to asnwer my question, and for that--thank you. But still, name shoudl be undefined no matter what right? So you put it in a function and it gave the correct value? I will try your validation method out. But this has left me with more questions than answers.


#4

...name shoudl be undefined no matter what right?

Yes, name should be undefined.

So you put it in a function and it gave the correct value?

It's doesn't matter where I put my code and I didn't give the correct value for variable name, name should be undefined but we have "". It's mean that name is defined as an empty string variable. And it's hard to explain how it can be.


#5

hmmmm, we need some moderator to help here, or a js wiz


#6

You will likely still need you JS wiz, but this might help get you started


#7

You just solved the problem dude, thank you! name is a propery in the window object. So that is why it was a declared empty string. Who woulda knew?


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.