What is the correct use of the "in" javascript operator?

Asked

Viewed 139 times

8

This code does not return anything, why? I am running in "window.onload event":

if(innerHeight in window){
     console.log('true');
}

I had already used the operator other times, and it worked, but now I got this... And then, someone can help me here?

1 answer

9


'innerHeight' in window

As you are looking for a property you should use a String, otherwise it’s like you’re searching for the contents of the variable innerHeight in window, or whatever you have is:

var alturaEmPixeis = windows.innerHeight; // isto é um numero
if (alturaEmPixeis in window){

and you should have

if ('innerHeight' in window){

to know if the object window has the name property innerHeight.

  • 1

    Thank you, Sergio! =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.