Javascript In: Using the 'In' Operator to Iterate Through Arrays and Objects

You can use the in operator in javascript to iterate through arrays or objects. This makes it particularly handy for viewing the properties and methods of an object, among other things. It's also useful for checking if particular hash keys exist or if an object has particular properties or methods. On top of all that, you can use it as an alternative to writing long-winded 'if' statements, where you repeatedly check a variable against a series of alternatives.

Iterating Through Arrays in Javascript Using 'In'
Using 'In' to View Hash Key-Value Pairs in Javascript
Viewing Object Properties and Methods Using 'In' in Javscript
Using 'In' to Check If A Hash Contains a Key
Use 'In' to Check If An Object Has a Property or Method
Use Javascript 'In' as an Alternative to Multiple Variable Tests With 'If'

Iterating Through Arrays in Javascript Using 'In'



The following code iterates through an array using in. Note that the loop variable is set to the index of each array element, not the element itself.

var fruits = ['apple', 'orange', 'banana'];

var text = '';

for(var i in fruits) {
    text += i + ': ' + fruits[i] + 'n';
}

alert(text);






Using 'In' to View Hash Key-Value Pairs in Javascript



The in operator also works with javascript hashes (associative arrays), allowing you to view the key-value pairs.

For example:

var fruits = {
    'apple': 'green',
    'banana': 'yellow',
    'orange': 'orange'
};

var text = '';
for(var i in fruits) {
    text += i + ': ' + fruits[i] + 'n';
}

alert(text);






Viewing Object Properties and Methods Using 'In' in Javscript



You can find out what properties and methods an object has in javascript using in. In the following code we define a simple javascript class, create an object of that class, then loop over the object's properties and methods.

function hello() {
    alert('Apples are ' + this.apple);
}

function fruits(){
    this.apple = 'green';
    this.orange = 'orange';
    this.strawberry = 'red';
    this.hello=hello;
}

var basket = new fruits();

var text = '';

for(var i in basket) {
    text += i + ': ' + basket[i] + 'n';
}

alert(text);







Using 'In' to Check If A Hash Contains a Key



In addition to helping you to iterate over things, the in operator can be used to check if a hash has a particular key:

var fruits = {
    'apple': 'green',
    'banana': 'yellow',
    'orange': 'orange'
};

if('apple' in fruits) {
    alert('Got apple!');
}






Use 'In' to Check If An Object Has a Property or Method



This also works with objects; you can even use in to check if a javascript object has a particular method.


function apple() {
    this.color = 'green';
    
    this.eat = function() {
        alert('mmmmm!');
    };
}

var fruit = new apple();

if('eat' in fruit) {
    alert('Got eat method!');
}

if('color' in fruit) {
    alert('Got color!');
}

















Use Javascript 'In' as an Alternative to Multiple Variable Tests With 'If'



Often in javascript you want to compare some variable to a series of possible values. As an alternative to a long if with multiple 'or' clauses, you can simply test if your variable is equal to one of the keys in a hash.

In other words, you can replace this:

var text = 'hello';

if(text == 'yes' || text == 'no' || text == 'hello') {
    alert('Match!');
}




with this:

var text = 'hello';

if(text in {'yes':1, 'no':1, 'hello':1}) {
    alert('Match!');
}