2

I have a question that is just bothering me. If I declare a var a and I then try to test it using the "in" operator I get a true result, if I do the same test using the dot notation I get a false result, for example...

var a;

if('a' in window) {
  console.log('a in window'); // this is written to the console
}

if (window.a) {
  console.log('window.a'); // nothing happening here
}

now I noticed that when I give a a value like so both outputs work... look at this and notice how I check if it doesn't exist:

var a;

if('a' in window) {
  console.log('a in window'); // this is written to the console
}

if (!window.a) {
  console.log('!window.a'); // this is written to the console
}

a = 1;

if (window.a) {
  console.log('window.a'); // this is written to the console
} 

So why does the dot notation only work when the variable is assigned a value? A silly question I know but so far I can't get a definite answer!

1
  • 2
    It's because window.a yeilds undefined - it evaulates to false Commented Feb 25, 2015 at 20:35

4 Answers 4

3

As Johan pointed out, the check is getting a falsey value.

if ('a' in window) console.log("a exists");
if (window.a) console.log("window.a is a truthy value");
if (!window.a) console.log("window.a is a falsey value");
if (typeof window.a != "undefined") console.log("window.a is not undefined");
Sign up to request clarification or add additional context in comments.

Comments

2

window.a is undefined before you give it a value, and

if (undefined) === if (Boolean(undefined))

Since Boolean(undefined) === false, the console.log within your if statement is not reached.

Comments

1

Go to your console and play like this:

var a;

undefined

'a' in window

true

window.a

undefined

a = 1

1

window.a

1

'a' in window

true

The in keyword checks if there is a variable in a context. So, an expression using in, will always return true or false based on its existence, not on its value.

The . notaction access the variable's value in a context. So, an expression using ., might return any value, including undefined, if it wasn't.

Now that you know what's being returned for each expression, mix it with what the evaluation done when each value is returned inside an if statement.

As you might know, undefined will evaluate to false, and both 1 and true will evaluate to true, resulting on the behavior you're facing.

Comments

0

The in operator in JavaScript checks if a key exists in an object by returning a boolean value. It verifies if the specified property is present within the object, simplifying key existence validation.

so in your case if("a" in window) is true since it is available in window

The Dot notation is a straightforward way to access object properties by using the dot followed by the property name. It is the most commonly used method to read and assign properties in JavaScript objects.

since a is not initialised yet and its var window.a will return undefined and undefined is calculated as false value

so window.a is evaluated as false and wont enter the if condition

if you assign var a = 10 and try to access window.a it returns 10 and is evaluated true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.