1Javascript Comparison Operators
2== Equal to
3=== Equal value and equal type
4!= Not equal
5!== Not equal value or not equal type
6> Greater than
7< Less than
8>= Greater than or equal to
9<= Less than or equal to
10? Ternary operator
11
1var a = 2;
2var b = 5;
3var c = 10;
4
5if (a === 3 || a === 2) {
6 console.log("TRUE");
7} else {console.log("FALSE");}
8if (a === 4 || b === 3 || c === 11) {
9 console.log("TRUE");
10} else {console.log("FALSE");}
11if (b === 5 || c != 10) {
12 console.log("TRUE");
13} else {console.log("FALSE");}
14
15/* Output:
16TRUE
17FALSE
18TRUE
19*/
1//|| is the or operator in JavaScript
2if(a == 1 || b != 'value'){
3 yourFunction();
4}
1/*OR operator:*/
2||
3
4// example:
5var a = 10;
6var b = 5;
7
8if(a > 7 or b > 7){
9 print("This will print!")
10}
11// Even though a is not less than 7, b is, so the program will print
12// the statement.
1// index.html
2<!DOCTYPE html>
3<html lang="en">
4 <head>
5 <meta charset="UTF-8" />
6 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
7 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
8 <title>js or operator</title>
9 </head>
10 <body>
11 <h1 class="display_msg">Add</h1>
12 <input type="text" name="num1" id="num1" /><br />
13 <input type="text" name="num2" id="num2" /><br /><br />
14 <button type="button" onclick="btnClicked()">Add</button>
15 <br /><br />
16 <span class="valid_details"></span>
17 <script src="app.js"></script>
18 </body>
19</html>
20
21// app.js
22function btnClicked() {
23 // value 1 and 2 from html input fields (with id 'num1' and 'num2')
24 // you can also use document.querySelector
25 let num1 = document.getElementById("num1").value;
26 let num2 = document.getElementById("num2").value;
27
28 // simple addition of num1 and 2 stored in 'result'
29 // use innerHTML to display the results in the browser
30 let result = (document.querySelector(".display_msg").innerHTML =
31 Number(num1) + Number(num2));
32
33 // some sort of validation using 'or' '||'
34 if (
35 num1 === "" ||
36 num2 === "" ||
37 num1 === 0 ||
38 num1 === "0" ||
39 num2 === 0 ||
40 num2 === "0"
41 ) {
42 result = document.querySelector(
43 ".display_msg"
44 ).innerHTML = `Invalid Input!!!`;
45 return result;
46 } else {
47 return result;
48 }
49
50 /*
51 * this will not print because return statement will basically end the function
52 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
53 */
54
55 console.log(result);
56}
57