-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathternary-operator.js
43 lines (33 loc) · 1.18 KB
/
ternary-operator.js
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
/*
-------------------------------------------------------------------------------------
Tutorial: Ternary Operator
-------------------------------------------------------------------------------------
*/
// The Ternary operator is way of make a conditional decisions
// Is a simple way of make an if-else block
// if-else block example
let age = 17;
let true_case = "You are an adult";
let false_case = "You are not an adult";
if( age > 18 ){
console.log(true_case);
} else{
console.log(false_case);
}
/*** Ternary Operator Syntax:
condition ?
"The condition is True" :
"The condition is false"
***/
// ternary-operator example
// this is exactly the same condition than before with the if-else block but it can be in one line instead of multiple lines
age = 42;
let msg = age > 18 ? true_case: false_case;
console.log(msg);
/*
-------------------------------------------------------------------------------------
Challenge: ternary-operator challenges
1. Use the ternary-operator to log a message if number is positive or not
2. Determine if an array is empty or not
-------------------------------------------------------------------------------------
*/