Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Use Conditional Statements On Objects?
Conditional statements allow you to execute different code blocks based on object properties and values. In JavaScript, you can use if, if-else, and switch statements to make decisions based on object data.
This article demonstrates how to apply conditional logic to JavaScript objects, checking their properties and values to control program flow.
Using if Statement with Objects
The if statement executes code when a condition evaluates to true. You can check object properties, existence, or values.
Syntax
if (condition) {
// code to execute if condition is true
}
Example
Here we check if a user object has sufficient balance for a purchase:
<!DOCTYPE html>
<html>
<body>
<script>
const user = {
name: "John",
balance: 1000
};
const shirtPrice = 1500;
if (user.balance < shirtPrice) {
document.write("Insufficient Balance! Need $" + (shirtPrice - user.balance) + " more.");
}
</script>
</body>
</html>
The output displays a message when the user's balance is insufficient for the purchase.
Using if-else Statement with Objects
The if-else statement provides an alternative action when the condition is false, making it perfect for object property validation.
Syntax
if (condition) {
// code if true
} else {
// code if false
}
Example
This example checks a person object's age property for voting eligibility:
<!DOCTYPE html>
<html>
<body>
<script>
const person = {
name: "Alice",
age: 20,
country: "USA"
};
if (person.age >= 18) {
document.write(person.name + " is eligible to vote in " + person.country);
} else {
document.write(person.name + " is not eligible to vote yet.");
}
</script>
</body>
</html>
The script evaluates the person's age and displays the appropriate voting eligibility message.
Using switch Statement with Objects
The switch statement compares an object property against multiple values, executing the matching case block.
Syntax
switch(expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// default code
}
Example
Here we use switch to handle different user roles from an object:
<!DOCTYPE html>
<html>
<body>
<script>
const employee = {
name: "Sarah",
role: "manager",
department: "Sales"
};
switch (employee.role) {
case "admin":
document.write("Full system access granted to " + employee.name);
break;
case "manager":
document.write("Department access granted to " + employee.name);
break;
case "employee":
document.write("Limited access granted to " + employee.name);
break;
default:
document.write("Access denied for " + employee.name);
}
</script>
</body>
</html>
The switch statement evaluates the employee's role and grants appropriate access permissions.
Checking Object Property Existence
You can use conditional statements to verify if object properties exist before accessing them:
<!DOCTYPE html>
<html>
<body>
<script>
const product = {
name: "Laptop",
price: 999
};
if ("discount" in product) {
document.write("Discounted price: $" + (product.price - product.discount));
} else {
document.write("Regular price: $" + product.price);
}
</script>
</body>
</html>
This prevents errors when accessing properties that might not exist on the object.
Conclusion
Conditional statements on objects enable dynamic decision-making based on object properties and values. Use if-else for binary conditions and switch for multiple value comparisons to create robust object-driven logic.
