in

Summary

Checks if an object/container has a property or if an index is in an array.

Syntax

property in object

Parameters

property
The name of a property to check for.
object
Any valid JavaScript object or JS++ container.

Description

The in operator returns true if the property exists in an object or container and false otherwise.

For arrays, the in operator returns true if a given index ("property") is in the array. For dictionaries, the in operator returns true if the dictionary contains the specified key ("property").

Examples

Using 'in' operator with System.Dictionary
1
2
3
4
5
6
7
8
9
import System;
 
Dictionary<int> names = {{
    "George": 25,
    "Paul": 26,
    "David": 40
}};
 
Console.log("Paul" in names); // true
Using 'in' operator with arrays
1
2
3
4
5
6
import System;
 
string[] names = [ "George", "Paul", "David" ];
 
Console.log(0 in names); // true
Console.log(5 in names); // false
Using 'in' operator on externals
1
2
3
4
5
6
7
8
9
var a = [ 1, 2, 3 ];
"0" in a; // true, "0" is index 0 here
 
var a = { b: 1 };
"b" in a; // true
 
function a() {}
a.b = 1;
"b" in a; // true
Incorrect usage of 'in'
1
2
3
4
5
6
7
8
var cart = [ "book", "toaster", "toy" ];
 
bool i0, i2, i3, ibook;
 
i0 = 0 in cart; // true
i2 = 2 in cart; // true
i3 = 3 in cart; // false
ibook = "book" in cart; // false. 'in' refers to element number, not contents

See Also

Share

HTML | BBCode | Direct Link