-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuthy-Falsy-Values-Ternary-Operator.js
More file actions
73 lines (56 loc) · 1.63 KB
/
Tuthy-Falsy-Values-Ternary-Operator.js
File metadata and controls
73 lines (56 loc) · 1.63 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Falsy values:
"" , 0 , -0 , BigInt 0n , false , NaN , null , undefined
Truthy values:
"0" , " " , "false" , [] , {} , function(){}
*/
const arr=[]
if(arr){ // empty arr make condition true --> shows array have elements
console.log("array contain some elements...")
}
else{
console.log("array is empty...")
}
// for checking array have elements or not:
const arr1=[]
if(arr.length === 0){
console.log("array have no elements")
}
else{
console.log("array have some elements")
}
// similarly this problem is also observed in objects. Its solution is :-
const myObj={}
if(myObj){ // myObj gives true value as condition --> same issue seen in array
console.log("myObj is not empty")
}
else{
console.log("myObj is empty")
}
const obj={}
if(Object.keys(obj).length === 0){
console.log("obj is empty...")
}
else{
console.log("obj is not empty...")
}
//some basic concepts...
console.log("false==0 : ",false==0)
console.log("''==0 : ",''==0)
console.log("false=='' : ",false=='')
//nullish coalescing operator (??) --> work with null/undefined
let variable;
variable=3 ?? 4
console.log("3 ?? 4 :- ",variable)
variable=null ?? 6
console.log("null ?? 6 :- ",variable)
variable=undefined ?? 10 ?? 20
console.log("undefined ?? 10 ?? 20 :- ",variable)
variable=15 ?? null
console.log("15 ?? null :- ",variable)
// Ternary operator
// syntax ---> (expression) ? (if true statement) : (if false statement)
const val=30>50 ? true : false
console.log(" val :- ",val)
const age=21
age>18 ? console.log("age is above 18") : console.log("age is less than 18")