-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.js
More file actions
52 lines (45 loc) · 1.42 KB
/
switch.js
File metadata and controls
52 lines (45 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// A switch statement allows you to check some conditions and perform a task depending on what condition is met.
// It acts much like an if statement, but it can be a bit more readable in some cases.
// How do switch statements work?
const programmingLanguage = 'Rust'
switch (programmingLanguage) {
case 'JavaScript':
console.log("It's JavaScript!")
// break; prevents running the next case statement
break
case 'PHP':
console.log("It's PHP!")
break
case 'C++':
console.log("It's C++!")
break
case 'Python':
console.log("It's Python!")
default:
console.log("I don't know that language!")
}
// You can usually do the same thing with if statements
if (programmingLanguage === 'JavaScript') {
console.log("It's JavaScript!")
} else if (programmingLanguage === 'PHP') {
console.log("It's PHP!")
} else if (programmingLanguage === 'C++') {
console.log("It's C++!")
} else if (programmingLanguage === 'Python') {
console.log("It's Python!")
} else {
console.log(`It's ${programmingLanguage}!`)
}
// Using multiple conditions within switch statements
switch (programmingLanguage) {
case 'JavaScript':
case 'PHP':
console.log("It's JavaScript or PHP!")
break
case 'C++':
case 'Python':
console.log("It's C++ or Python!")
break
default:
console.log("I don't know that language!")
}