Skip to content

Commit 0871231

Browse files
committed
Updated Conditionals in JS
1 parent ef42a51 commit 0871231

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

conditionals.js

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
1+
CONDITIONALS IN JAVASCRIPT
12

2-
shrivastav
3+
-- Control Flow
4+
5+
~ Control flow is the order in which statements are executed in a program.
6+
~ The default control flow is for statements to be read and executed in order from left-to-right, top-to-bottom in a program file.
7+
~ Control structures such as conditionals (if statements and the like) alter control flow by only executing blocks of code if certain conditions are met.
8+
~ These structures essentially allow a program to make decisions about which code is executed as the program runs.
9+
10+
11+
-- Logical Operator ||
12+
The logical OR operator || checks two values and returns a boolean. If one or both values are truthy, it returns true.
13+
If both values are falsy, it returns false.
14+
15+
A B A || B
16+
false false false
17+
false true true
18+
true false true
19+
true true true
20+
//examples-
21+
true || false; // true
22+
10 > 5 || 10 > 20; // true
23+
false || false; // false
24+
10 > 100 || 10 > 20; // false
25+
26+
27+
-- If Statement
28+
An if statement accepts an expression with a set of parentheses:
29+
30+
If the expression evaluates to a truthy value, then the code within its code body executes.
31+
If the expression evaluates to a falsy value, its code body will not execute.
32+
//example
33+
const isMailSent = true;
34+
35+
if (isMailSent) {
36+
console.log('Mail sent to recipient');
37+
}
38+
39+
40+
-- Else Statement
41+
An else block can be added to an if block or series of if-else if blocks. The else block will be executed only if the if condition fails.
42+
43+
//example
44+
const isTaskCompleted = false;
45+
if (isTaskCompleted) {
46+
console.log('Task completed');
47+
} else {
48+
console.log('Task incomplete');
49+
}
50+
51+
52+
-- Else If Clause
53+
After an initial if block, else if blocks can each check an additional condition. An optional else block can be added after the else if block(s) to run by default if none of the conditionals evaluated to truthy.
54+
55+
//example
56+
const size = 10;
57+
if (size > 100) {
58+
console.log('Big');
59+
} else if (size > 20) {
60+
console.log('Medium');
61+
} else if (size > 4) {
62+
console.log('Small');
63+
} else {
64+
console.log('Tiny');
65+
}
66+
// Print: Small
67+
68+
69+
-- Logical Operator &&
70+
The logical AND operator && checks two values and returns a boolean. If both values are truthy, then it returns true.
71+
If one, or both, of the values is falsy, then it returns false.
72+
//examples
73+
true && true; // true
74+
1 > 2 && 2 > 1; // false
75+
true && false; // false
76+
4 === 4 && 3 > 1; // true
77+
78+
79+
-- Logical Operator !
80+
The logical NOT operator ! can be used to do one of the following:
81+
Invert a Boolean value.
82+
Invert the truthiness of non-Boolean values.
83+
//examples
84+
let lateToWork = true;
85+
let oppositeValue = !lateToWork;
86+
87+
console.log(oppositeValue);
88+
// Prints: false

0 commit comments

Comments
 (0)