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
Selected Reading
Can we have a return statement in a JavaScript switch statement?
Yes, you can use return statements in a JavaScript switch statement, but only when the switch is inside a function. The return statement will immediately exit the function with the specified value, making break statements unnecessary.
How Return Statements Work in Switch
When a return statement is executed in a switch case, it immediately exits the function and returns the value. This eliminates the need for break statements since the function execution stops.
Example: Day Name Function
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Return in Switch Statement</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
padding: 20px;
}
.result {
font-size: 18px;
font-weight: 500;
color: green;
margin: 10px 0;
}
input, button {
padding: 8px;
margin: 5px;
}
</style>
</head>
<body>
<h1>Return Statement in JavaScript Switch</h1>
<div>
Enter day (1-7): <input type="text" class="day">
<button class="btn">CHECK</button>
</div>
<div class="result"></div>
<script>
function getDayName(dayNum) {
switch (dayNum) {
case 1:
return "It's Monday";
case 2:
return "It's Tuesday";
case 3:
return "It's Wednesday";
case 4:
return "It's Thursday";
case 5:
return "It's Friday";
case 6:
return "It's Saturday";
case 7:
return "It's Sunday";
default:
return "Enter a value between 1-7";
}
// This code will never execute due to return statements
console.log("This line is unreachable");
}
document.querySelector(".btn").addEventListener("click", () => {
const dayInput = document.querySelector(".day");
const result = document.querySelector(".result");
const dayNumber = parseInt(dayInput.value);
result.textContent = getDayName(dayNumber);
});
</script>
</body>
</html>
Comparison: Return vs Break
| Method | Use Case | Code After Switch |
|---|---|---|
return |
Inside functions | Never executes |
break |
General switch usage | Continues execution |
Key Points
- Return statements can only be used in switch statements that are inside functions
- When return executes, the function immediately exits with the returned value
- No break statements are needed when using return
- Code after the switch statement becomes unreachable when using return
Console Example
function getGrade(score) {
switch (true) {
case score >= 90:
return "A";
case score >= 80:
return "B";
case score >= 70:
return "C";
case score >= 60:
return "D";
default:
return "F";
}
// This line never executes
console.log("Unreachable code");
}
console.log(getGrade(85)); // B
console.log(getGrade(92)); // A
console.log(getGrade(55)); // F
B A F
Conclusion
Return statements in switch cases provide a clean way to exit functions immediately with a value. They eliminate the need for break statements and make the code more concise when the switch is the primary logic of the function.
Advertisements
