Open In App

Js Equivalent to Python In

Last Updated : 27 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, it is used for iterating over elements, checking membership and even used for conditional checks. JavaScript offers similar functionality with different syntax and behavior. This article discusses how Python's in keyword works in different contexts and how to achieve similar behavior in JavaScript.

The in keyword in Python is used in several ways, including:

  • Iteration: To iterate over elements in a collection (e.g., list, string).
  • Condition Checking: To check if a value exists in an iterable (list, tuple, string, etc.).
  • Dictionary Key Checking: To check if a key exists in a dictionary.

Example:

Python
# List for iteration
l = [1, 2, 3, 4, 5]

# Dictionary for key checking
person = {"name": "Alice", "age": 30}

# Iteration
for x in l:
    print(x)

# Condition Checking
if 3 in l:
    print("3 is in the list")

# Dictionary Key Checking: To check if a key exists in the dictionary
if "age" in person:
    print("Found")

Output
1
2
3
4
5
3 is in the list
Found

Explanation:

  • in keyword is used in a for loop to iterate over the list l and print each element.
  • in keyword checks if the value 3 exists in the list l.
  • in keyword checks if the key "age" exists in the dictionary person.

Let's explore JavaScript's in Equivalent:

JavaScript Equivalent to Python in for Iteration

In JavaScript, we can use for...of for iterating over elements in arrays or strings, which is similar to Python's in used in a for loop.

Iterating Over an Array in JavaScript

JavaScript
const l = [1, 2, 3, 4, 5];
for (const x of l) {
    console.log(x);
}

Output
1
2
3
4
5

Explanation:

  • The for...of loop in JavaScript is used to iterate over the array l, similar to Python's for x in l.

Iterating Over a String in JavaScript

JavaScript
const s = "Hello, Shivang";
for (const char of s) {
    console.log(char);
}

Output
H
e
l
l
o
,
 
S
h
i
v
a
n
g

Explanation:

  • The for...of loop in JavaScript can be used to iterate over the characters in a string, similar to Python's for x in s.

JavaScript Equivalent to Python in for Conditions

In JavaScript, we can use methods like includes() for arrays or strings to replicate this conditional checking.

Using includes() in a Conditional Statement in JavaScript

JavaScript
const l = [1, 2, 3, 4, 5];
const x = 3;
if (l.includes(x)) {
    console.log(`${x} is in the list.`);
} else {
    console.log(`${x} is not in the list.`);
}

Output
3 is in the list.

Explanation:

  • The includes() method in JavaScript checks if x exists in the array l, similar to Python's in used in the if statement.

Using includes() for Substrings in JavaScript

JavaScript
const s = "Hello, Shivang";
const substr = "Shivang";
if (s.includes(substr)) {
    console.log(`${substr} is in the string.`);
} else {
    console.log(`${substr} is not in the string.`);
}

Output
Shivang is in the string.

Explanation:

The includes() method checks if the substring "Shivang" exists in the string s, similar to Python's in keyword when used with strings.

JavaScript Equivalent to Python in for Objects (Keys)

JavaScript uses the in operator to check if a key exists in an object. This is very similar to Python's in when used with dictionaries.

Checking Keys in an Object in JavaScript

JavaScript
const obj = { name: 'Shivang', age: 25 };
if ('name' in obj) {
    console.log("Key 'name' is in the object.");
} else {
    console.log("Key 'name' is not in the object.");
}

Output
Key 'name' is in the object.

Explanation:

  • The in operator checks if the key 'name' exists in the object obj, similar to Python's use of in for dictionaries.

Next Article
Article Tags :
Practice Tags :

Similar Reads