Hey there! I'm a supplier of switches, and today I wanna chat about how to use a switch statement with objects in JavaScript. It might sound a bit technical, but I'll break it down in a way that's easy to understand.
First off, let's quickly go over what a switch statement is in JavaScript. A switch statement is a control flow statement that allows you to evaluate an expression and match it against multiple cases. It's kind of like a series of if - else statements but in a more organized way.
let fruit = 'apple';
switch (fruit) {
case 'apple':
console.log('It's an apple!');
break;
case 'banana':
console.log('It's a banana!');
break;
default:
console.log('I don't know this fruit.');
}
In this simple example, the switch statement checks the value of the fruit variable. If it's 'apple', it runs the code inside that case block. The break statement is crucial here; it stops the execution of the switch statement once a match is found. If there's no match, the default block runs.
Now, let's talk about using a switch statement with objects. Objects in JavaScript are a collection of key - value pairs. They're super useful for storing related data. Suppose we have an object that represents different types of switches we supply.
const switchTypes = {
single: 'Single Powerpoint Switch',
doubleExtra: 'Double Powerpoint with Extra Switch',
weatherproof: 'Weatherproof Single Powerpoint Switch'
};
let selectedSwitch = 'single';
switch (selectedSwitch) {
case 'single':
console.log(`You've selected a [Single Powerpoint Switch](/electrical/switch/single-powerpoint-switch.html).`);
break;
case 'doubleExtra':
console.log(`You've selected a [Double Powerpoint with Extra Switch](/electrical/switch/double-powerpoint-with-extra-switch.html).`);
break;
case 'weatherproof':
console.log(`You've selected a [Weatherproof Single Powerpoint Switch](/electrical/switch/weatherproof-single-powerpoint-switch.html).`);
break;
default:
console.log('Sorry, we don't have that type of switch.');
}
In this code, we first define an object switchTypes that maps short names to the full names of our switch products. Then, we use a switch statement to check the value of selectedSwitch. Depending on the value, we can display information about the selected switch, including a link to its product page.
One of the benefits of using a switch statement with objects is that it makes the code more readable and maintainable. If we need to add a new type of switch in the future, we can simply add a new key - value pair to the switchTypes object and a new case in the switch statement.
Let's take a more practical example. Suppose we have a function that takes an order for a switch and returns a confirmation message.
function processSwitchOrder(switchType) {
let message;
switch (switchType) {
case 'single':
message = `Your order for a [Single Powerpoint Switch](/electrical/switch/single-powerpoint-switch.html) has been received.`;
break;
case 'doubleExtra':
message = `Your order for a [Double Powerpoint with Extra Switch](/electrical/switch/double-powerpoint-with-extra-switch.html) has been received.`;
break;
case 'weatherproof':
message = `Your order for a [Weatherproof Single Powerpoint Switch](/electrical/switch/weatherproof-single-powerpoint-switch.html) has been received.`;
break;
default:
message = 'Sorry, we cannot process your order for this switch type.';
}
return message;
}
let orderType = 'doubleExtra';
console.log(processSwitchOrder(orderType));
This function processSwitchOrder takes a switchType as an argument. Inside the switch statement, it creates a confirmation message based on the type of switch ordered. If the switchType is not recognized, it returns an error message.
Another thing to note is that the values in the case statements must match the values of the keys you're checking against. JavaScript uses strict equality (===) to compare the value in the switch expression with the values in the case statements. So, make sure the types and values are exactly the same.
Let's say we want to handle different actions based on the switch type in a more complex scenario. Maybe we have a function that calculates the price of the switch based on its type.
function calculateSwitchPrice(switchType) {
let price;
switch (switchType) {
case 'single':
price = 10;
break;
case 'doubleExtra':
price = 20;
break;
case 'weatherproof':
price = 15;
break;
default:
price = 0;
}
return price;
}
let type = 'weatherproof';
let switchPrice = calculateSwitchPrice(type);
console.log(`The price of the [Weatherproof Single Powerpoint Switch](/electrical/switch/weatherproof-single-powerpoint-switch.html) is $${switchPrice}.`);
In this example, the calculateSwitchPrice function uses a switch statement to determine the price of the switch based on its type. Then, it returns the calculated price.
If you're working with a large number of switch types, you might want to consider using an object - based approach instead of a long switch statement. You can create an object where the keys are the switch types and the values are functions that perform the necessary actions.
const switchActions = {
single: function() {
return `You've selected a [Single Powerpoint Switch](/electrical/switch/single-powerpoint-switch.html).`;
},
doubleExtra: function() {
return `You've selected a [Double Powerpoint with Extra Switch](/electrical/switch/double-powerpoint-with-extra-switch.html).`;
},
weatherproof: function() {
return `You've selected a [Weatherproof Single Powerpoint Switch](/electrical/switch/weatherproof-single-powerpoint-switch.html).`;
}
};
let chosenSwitch = 'doubleExtra';
if (switchActions[chosenSwitch]) {
console.log(switchActions[chosenSwitch]());
} else {
console.log('Sorry, we don't have that switch type.');
}
This approach can be more flexible and easier to manage as your application grows.


In conclusion, using a switch statement with objects in JavaScript is a great way to handle different cases based on the properties of an object. Whether you're creating order confirmation messages, calculating prices, or performing other actions related to the switch types we supply, it provides a clear and organized way to write your code.
If you're interested in purchasing any of our switches, whether it's the Single Powerpoint Switch, Double Powerpoint with Extra Switch, or Weatherproof Single Powerpoint Switch, feel free to reach out to us for a quote and to start the procurement process. We're here to help you find the right switch for your needs.
References:
- JavaScript Documentation on MDN Web Docs
- "JavaScript: The Definitive Guide" by David Flanagan





