Js Equivalent to Python In
Last Updated :
27 Dec, 2024
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")
Output1
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);
}
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);
}
OutputH
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.`);
}
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.`);
}
OutputShivang 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.");
}
OutputKey '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.
Similar Reads
Javascript equivalent to Python Dictionary A dictionary is a powerful data structure allowing us to store and access data using keys quickly. In Python, we use a dictionary to store key-value pairs. In JavaScript, the closest equivalent to a Python dictionary is the Object or Map. The choice between a JavaScript Object or Map depends on the
3 min read
Working With JSON Data in Python JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The tex
6 min read
Introduction to Python3 Python is a high-level general-purpose programming language. Python programs generally are smaller than other programming languages like Java. Programmers have to type relatively less and indentation requirements of the language make them readable all the time. Note: For more information, refer to P
3 min read
Python Built in Functions Python is the most popular programming language created by Guido van Rossum in 1991. It is used for system scripting, software development, and web development (server-side). Web applications can be developed on a server using Python. Workflows can be made with Python and other technologies. Databas
6 min read
JSON Formatting in Python JSON (JavaScript Object Notation) is a popular data format that is used for exchanging data between applications. It is a lightweight format that is easy for humans to read and write, and easy for machines to parse and generate. Python Format JSON Javascript Object Notation abbreviated as JSON is a
3 min read
Integrating Java with Python While programming in a language, a developer may feel the need to use some functionality that may have better support in another language. For example, suppose, an application has already been developed in Java, and we wish to use it in Python code. To invoke an existing Java application in Python,
4 min read