Published 2022. 11. 17. 09:53

Comparison Operators

== equal to x == 8 false
x == 5 true
x == "5" true
=== equal value and equal type x === 5 true
x === "5" false
!= not equal x != 8 true
!== not equal value or not equal type x !== 5 false
x !== "5" true
x !== 8 true
> greater than x > 8 false
< less than x < 8 true
>= greater than or equal to x >= 8 false
<= less than or equal to x <= 8 true

 

 

 

Comparing Different Types

 JavaScript will convert the string to a number when doing the comparison.

When comparing two strings, "2" will be greater than "12", because (alphabetically) 1 is less than 2.

2 < 12 true
2 < "12" true
2 < "John" false
2 > "John" false
2 == "John" false
"2" < "12" false
"2" > "12" true
"2" == "12" false

 

 

 

The Nullish Coalescing Operator (??)

The ?? operator returns the first argument if it is null or undefined. Otherwise it returns the second.

<p id="demo"></p>

<script>
let name = null;
let text = "missing";
let result = name ?? text;
document.getElementById("demo").innerHTML = "The name is " + result; 
</script>
The name is missing

 

 

 

The Optional Chaining Operator (?.)

<p>Car name is:</p>
<p id="demo"></p>

<script>
const car = {type:"Fiat", model:"500", color:"white"};
let name = car?.name;
document.getElementById("demo").innerHTML = name;
</script>
Car name is:
undefined

 

'JavaScript' 카테고리의 다른 글

JavaScript - Iterables  (0) 2022.11.21
JavaScript - For In, For of  (0) 2022.11.17
JavaScript - Booleans  (0) 2022.11.17
JavaScript - Math Object  (0) 2022.11.17
JavaScript - Set Date Methods  (0) 2022.11.17
복사했습니다!