Javascript undefined, truthy vs typeof operator -


considering following code:

//the var test has not been declared yet  console.log((typeof test)); // "undefined" console.log(test); //"uncaught referenceerror: test not defined" 

why second console.log statement throw referenceerror , first displays undefined.

because test undefined.

in first console.log you're asking system tell type of variable. looks through current scope chain find reference variable may infer type.

when doesn't find variable, receives undefined primitive. has type, i'm sure you've guessed, of undefined.

the second time you're asking print out value of undefined variable. since variable not defined (there no reference in current scope chain), error you're trying access data doesn't exist, not infer type.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -