-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrelationalOperators.js
More file actions
29 lines (23 loc) · 907 Bytes
/
relationalOperators.js
File metadata and controls
29 lines (23 loc) · 907 Bytes
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
let x = "Pen"
let y = "Book"
let result1 = x > y // COMPARING ASCII VALUES OF "PEN" AND "BOOK"
console.log(result1)
let p = "2"
let q = 1
let result2 = p > q //CONVERTING STRING IN NUMBER
console.log(result2)
let a = 5
let b = "5"
let result3 = a == b // IT WILL NOT CHECK THE TYPE OF THE VARIABLES. IT WILL CONVERT THE STRING INTO NUMBER BEFORE CHECKING
console.log(result3) // THAT'S WHY WE SHOUD NEVER DO IT FOR COMPARING, WE SHOULD USE "===", IT WILL CHECK THE DATA AS WELL AS TYPE OF THE VARIABLE
let c = 5
let d = "5"
let result = c === d // """ //
console.log(result)
//below example is why using "==" for comparing is dangerous in javascript
let i = ""
let j = false
let result4 = i == j
console.log(result4) // IT WILL GIVE THE OUTPUT TRUE BECAUSE IT WILL CONVERT THE EMPTY STRING INTO false
let result5 = i === j
console.log(result5) // IT IS THE RIGHT WAY