-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.8_Conditionalbranching.html
71 lines (70 loc) · 2.78 KB
/
1.8_Conditionalbranching.html
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html>
<head>
<script>
// Conditional branching: if,?
//if statement: used to check if a condition is true or not. If true a block of code is executed
let year=prompt('In which year ECMAScript-2015 specification published?');
if (year == 2015)
{
alert('You are right!');
}
/*Boolean coverstion: by default, the condition results as a boolean value i.e., true/false.
Recall type conversions:
Falsy values: 0,"",null, undefined,NaN become false
Truthy values: rest all become true
*/
if(0)
{
// this code will never execute as condition will always be false
alert(false);
}
if(1)
{
// this code will always execute as condition will always be true
alert(true);
}
//if-else statement: similar to if, a block will be executed if true, another will be executed when false
year=prompt('In which year ECMAScript-2015 specification published?');
if (year == 2015)
{
alert('You are right!');
}
else
{
alert('You are wrong!');
}
//if-else if statement: used to check several conditions
year=prompt('In which year ECMAScript-2015 specification published?');
if (year > 2015)
{
alert('Too late!');
}
else if(year < 2015)
{
alert('Too early!');
}
else
{
alert('Correct!');
}
/* Conditional operator '?': works similar to if else, is used to make the code shorter
let result = condition ? value 1 : value 2
It works such that if the condition is true then value 1 will be executed, otherwise value 2 will be executed
*/
let accessAllowed;
let age = prompt('How old are you?', '');
alert(accessAllowed = (age > 18) ? true : false);
// Here, ? can be omitted as expression comparision by default returns a boolean
alert(accessAllowed = (age > 18));
/* Multiple '?': works like nested if/if else if. When we want to check multiple conditions
*/
age=prompt('age?',18);
let message = (age < 3) ? 'Hi, baby!' :
(age < 18) ? 'Hello!' :
(age < 100) ? 'Greetings' :
'What an unusual age!';
alert(message);
</script>
</head>
</html>