in
a Conditional test
in
represents something inside of something else. So we can use it as a conditional test to check if something is in
something else.
In an Object
, we could check if a property name exists inside it:
var my_object = {first:'a',second:'b',third:'c',fourth:'d'}
console.log( 'first' in my_object ) // returns true
console.log( 'me' in my_object ) // returns false
In an Array
, we could check if a key exists inside it:
This array has five keys in
it.
var my_array = [ 'a' , 'b' , 'c' , 'd' , 'e' ]
Each element can then be referenced later with a numeric key like so:
my_array[0] = 'a'
my_array[1] = 'b'
my_array[2] = 'c'
my_array[3] = 'd'
my_array[4] = 'e'
console.log( 0 in my_array ) // returns true
console.log( 1 in my_array ) // returns true
console.log( 2 in my_array ) // returns true
console.log( 3 in my_array ) // returns true
console.log( 4 in my_array ) // returns true
console.log( 5 in my_array ) // returns false
So the in
operator refers to the conditionality of an Object
containing a certain property name.
in
looping Arrays with numeric keys
Lets say you have an array like so:
var my_array = [ 'a' , 'b' , 'c' , 'd' , 'e' ]
You can use a for loop to iterate through each of these:
for(var key=0; key<my_array.length; key++){
var this_element = my_array[key]
}
But you can also do this with the in
operator:
for(var key in my_array){
var this_element = my_array[key]
}
So the in
operator refers to each key inside of an Array
.
in
looping Objects with NON-numeric keys
The in
operator is commonly used with a for
loop through Object
property names in JavaScript.
This is especially useful for Objects
that have properties, which are somewhat treated like Array
values.
A lot of times you many not be sure how many properties exist inside of your object or even what the property names are.
For example, this is a typical Object:
var my_object = {first:'a',second:'b',third:'c',fourth:'d'}
Each property can then be referred to with their property name, like so:
my_object['first'] = 'a'
my_object['second'] = 'b'
my_object['third'] = 'c'
my_object['fourth'] = 'd'
So here, we see that the properties are not defined with simple integer numbers like an Array
. Therefore we must use the in
operator to walk us through the Object
properties.
Object
properties rarely have numeric keys like JavaScript Arrays
do. So there is no way to conduct a traditional numeric for
loop.
So instead of defining a for
loop traditionally with definite numeric bounds, you use in
to write the following without including any definite bounds into the for
loop parameters, like so:
for(var element in my_array){}
So the in
operator refers to each property name of an Object
.
Dangers of using in
with for
Objects have inherent properties.
For example, an Array
is a type of Object
and all Arrays
have the length
property in their object prototype.
Just by defining an empty object, there are certain properties that come with it. A for
loop using in
will iterate over these undesired properties and 'could' break your code.
So, even in an empty Array
:
console.log( 'length' in new Array() ) // returns true
This same principle may cause a for
loop to iterate over undesired properties, depending on your web-browser and version.