|
| 1 | +// Operator |
| 2 | +// a character or group of characters that tells the compiler or interpreter to perform a specific action |
| 3 | +// Arithmetics Oprators: + - * / % |
| 4 | + |
| 5 | +var x = 10; |
| 6 | +var y = 39; |
| 7 | + |
| 8 | +console.log(x+y); |
| 9 | +console.log(y-x); |
| 10 | +console.log(x*y); |
| 11 | +console.log(y/x); |
| 12 | +console.log(x%y); |
| 13 | + |
| 14 | + |
| 15 | +var z = x + y; |
| 16 | +console.log(z); |
| 17 | + |
| 18 | + |
| 19 | +// Exponential Operator |
| 20 | +x = 2**2; |
| 21 | +console.log(x); |
| 22 | + |
| 23 | +// increment and decrement |
| 24 | +var a = 30; |
| 25 | +var b = 20; |
| 26 | + |
| 27 | + |
| 28 | +console.log(a++); |
| 29 | +console.log(++b); |
| 30 | + |
| 31 | +console.log(a); |
| 32 | + |
| 33 | +//First assign the value in b thne increment a value |
| 34 | +c = a++; |
| 35 | +console.log(c); |
| 36 | +console.log(a); |
| 37 | + |
| 38 | +//Fist increment value then assign the value |
| 39 | +c = ++b; |
| 40 | +console.log(c); |
| 41 | +console.log(b); |
| 42 | + |
| 43 | +// precedence of aritmethic equation |
| 44 | +// () = > first |
| 45 | +// ^ => second |
| 46 | +// / * => third |
| 47 | +// + - => fourth |
| 48 | + |
| 49 | +var eq = 8 + 3 - 4 * 3 / 2 ** 2 |
| 50 | +console.log(eq); |
| 51 | + |
| 52 | + |
| 53 | +// String Operator: |
| 54 | + |
| 55 | +// concat tow String |
| 56 | + |
| 57 | +var word1 = "Hello, "; |
| 58 | +var word2 = "World"; |
| 59 | + |
| 60 | +var word3 = word1 + word2; |
| 61 | +console.log(word3); |
| 62 | + |
| 63 | +// It concating follow left right. If possible to adding and |
| 64 | +// subtraction or any other aritmethic operation it calculate it then concat with string |
| 65 | + |
| 66 | +var word4 = 10 - 3 + 3 + "hello world"; |
| 67 | +console.log(word4); |
| 68 | + |
| 69 | +var word5 = 8 + 3 - 4 * 3 / 2 ** 2 + " world"; |
| 70 | +console.log(word5); |
| 71 | + |
| 72 | +//If not any scope to operate arithmetic operations it concat with string |
| 73 | + |
| 74 | +word5 = "hello world" + 8 + 3; |
| 75 | +console.log(word5); |
| 76 | + |
| 77 | +//If two string is a number it doing substraction operations |
| 78 | +word5 = "23" - "2"; |
| 79 | +console.log(word5); |
| 80 | + |
| 81 | +word5 = "23" - 3; |
| 82 | +console.log(word5); |
| 83 | + |
| 84 | + |
| 85 | +word5 = "23" + 3; |
| 86 | +console.log(word5); |
| 87 | + |
| 88 | +// If one operand a char and operate with sub operation result will be not a number |
| 89 | + |
| 90 | +word5 = "d" - 3; |
| 91 | +console.log(word5); |
| 92 | + |
| 93 | +// Conditional Operators |
| 94 | +// Comparison operators can be used in conditional statements to compare values and take action depending on the result: |
| 95 | +// Result will be of comparison operation is either true or false |
| 96 | +//Comarison opeator are: ==, ===, !=, !==, >, <, <=, >= |
| 97 | + |
| 98 | +var x = 3; |
| 99 | +var y = "3"; |
| 100 | +//== operator check one the value is equal or not. if equal it can be true otherwise false |
| 101 | +console.log(x==y); |
| 102 | + |
| 103 | +//=== operator check value and type. if equal it can be true otherwise false |
| 104 | +console.log(x===y); |
| 105 | + |
| 106 | +x = 4; |
| 107 | +y = 4; |
| 108 | +console.log(x==y); |
| 109 | + |
| 110 | +// != operator check the value in not equal to other operator. It check only value not type |
| 111 | +x = 3; |
| 112 | +y = 4; |
| 113 | + |
| 114 | +console.log(x!=y); |
| 115 | + |
| 116 | + |
| 117 | +// != operator check the value in not equal to other operator. It check value and type both |
| 118 | +x = 3; |
| 119 | +x = "3"; |
| 120 | +y = 3; |
| 121 | +console.log(x!=y); |
| 122 | +console.log(x!==y); |
| 123 | + |
| 124 | +// compare with two number greter than equal or greater, less then equal or less |
| 125 | +console.log(x>=y); |
| 126 | +console.log(x>y); |
| 127 | +console.log(x<=y); |
| 128 | +console.log(x<y); |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +// Logical Operators |
| 133 | +// Logical operators are used to determine the logic between variables or values. |
| 134 | +x = 6; |
| 135 | +y = 3; |
| 136 | + |
| 137 | +console.log(x < 10 && y > 1); |
| 138 | +console.log((x == 5 || y == 5)); |
| 139 | +console.log((x == 5 || y == 3)); |
| 140 | +console.log((x == 6 && !(y == 3))); |
| 141 | + |
| 142 | +// Conditional (Ternary) Operator |
| 143 | +// JavaScript also contains a conditional operator that assigns a value to a variable based on some condition. |
| 144 | +age = 32; |
| 145 | +let voteable = (age < 18) ? "To Young" : "Old enough"; |
| 146 | +console.log(voteable); |
| 147 | + |
| 148 | + |
| 149 | +//Compare with tree number; |
| 150 | +let p = 34; |
| 151 | +let q = 42; |
| 152 | +let r = 42; |
| 153 | + |
| 154 | +let maxNumb = (p >= q) ? (p >= r) ? "P is greater": "R is greater" : (q >= r) ? "Q is greater" : "R is greater"; |
| 155 | + |
| 156 | +console.log(maxNumb); |
| 157 | + |
| 158 | +// Assignment Operator |
| 159 | +// Assignment operator are =, +=, -=, *=, /=,%=, **== |
| 160 | + |
| 161 | +x = 10; |
| 162 | +x += 10; |
| 163 | +console.log(x); |
| 164 | +x -= 10; |
| 165 | +console.log(x); |
| 166 | +x *= 10; |
| 167 | +console.log(x); |
| 168 | +x /= 10; |
| 169 | +console.log(x); |
| 170 | +x %= 10; |
| 171 | +console.log(x); |
| 172 | +x = 2; |
| 173 | +x **= 10; |
| 174 | +console.log(x); |
| 175 | + |
| 176 | + |
| 177 | +// Excercise |
| 178 | + |
| 179 | +var celTemp = prompt("Enter your temperature in celcius"); |
| 180 | +var farenheit = 1.8 * celTemp + 32; |
| 181 | +alert("Farenheit: " + farenheit); |
| 182 | +console.log("Task Complete"); |
0 commit comments