jk1
jk1

Reputation: 108

Use of "in" while declaring JS variables

Thanks in advance for your patience... Really trying to fill in weird gaps in my JS knowledge...

Analyzing other people's scripts and came across:

Var isTouch = "ontouchstart" in window;

What is the "in window" part doing? I've googled several basic variable declaration tutorials and it's not mentioned, and "in" is such a basic word, it's been hard to find answers.

Thanks,

JK

Upvotes: 2

Views: 97

Answers (2)

user578895
user578895

Reputation:

isTouch is now simply a boolean (true or false), as the in operator returns a boolean:

var foo = {
   bar : 42
},

hasBar  = 'bar'  in foo,  // true
hasFoob = 'foob' in foo;  // false

Upvotes: 1

SLaks
SLaks

Reputation: 887469

The expression someString in someObject returns a boolean indicating whether the object has a property by that name.
Spec

Your code, other than having a miscapitalized Var, sets isTouch to true if window has an ontouchstart property.

Upvotes: 2

Related Questions