-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathif-statement.js
More file actions
42 lines (34 loc) · 1.11 KB
/
if-statement.js
File metadata and controls
42 lines (34 loc) · 1.11 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
// 1) What is an 'if statement'?
// If statements allow your program to go down different paths depending on certain data within your program.
// So, you give your program some condition, and depending on what that condition evaluates to, your program will go down different paths.
// 2) How if statements work in JavaScript
const count = 4
// The if condition needs to evaluate to a boolean
if (count < 5) {
console.log('The count is less than 5')
}
else if (count < 10 ) {
console.log('The count is less than 4, but less than 10')
}
else {
console.log('The count is greater than 9')
}
// You can have multiple conditions within parens
if (count < 5 && count > 0) {
console.log('The count is less than 5 and greater than 0')
}
// Preferences for early-return to if/else statements
function getCount(count) {
if (count < 5) {
return 'The count is less than 5'
}
else if (count < 10 ) {
return 'The count is less than 4, but less than 10'
}
else {
return 'The count is greater than 9'
}
}
console.log(getCount(4))
console.log(getCount(7))
console.log(getCount(12))