@@ -824,9 +824,10 @@ function checkAge(age) {
824824` ` `
825825
826826Rewrite it, to perform the same, but without ` if ` , in a single line.
827- Make two variants of ` checkAge` :
828- 1. Using a question mark operator ` ? `
829- 2. Using OR ` || `
827+ Make two variants of ` checkAge` :
828+
829+ 1. Using a question mark operator ` ? `
830+ 2. Using OR ` || `
830831
831832` ` ` JavaScript
832833function checkAge (age ) {
@@ -1172,3 +1173,147 @@ let isTeaWanted = confirm("Do u want tea?");
11721173alert( "Visitor: " + userName );
11731174alert( "Tea wantd: " + isTeaWanted );
11741175` ` `
1176+
1177+ #### Operators
1178+
1179+ JavaScript supports the following operators:
1180+
1181+ - ** Arithmetical**
1182+
1183+ Regular: ` + - * /` , also ` %` for the remainder and ` **` for power of a number.
1184+
1185+ The binary plus ` +` concatenates strings . And if any of the operands is a string, the other one is convertd too.
1186+
1187+ ` ` ` JavaScript
1188+ alert('1' + 2); // '12', string
1189+ alert(1 + '2'); // '12', string
1190+ ` ` `
1191+
1192+ - ** Assignments**
1193+
1194+ There is a simple assigment: ` a = b` and combined ones like ` a *= 2`
1195+
1196+ - ** Bitwise**
1197+
1198+ Bitwise operators work with 32 - bit integers at the lowest, bit- level.
1199+
1200+ - ** Conditional**
1201+
1202+ The only operator with three parameters: ` cord ? resultA : resultB` .
1203+
1204+ - ** Logical operators**
1205+
1206+ Logical AND ` &&` and OR ` ||` perform short- circuit evaluation and then return the value it stopped.
1207+ Logical NOT ` !` converts the operand to boolean type and returns the inverse value.
1208+
1209+ - ** Nullish coalescing operator**
1210+
1211+ The ` ??` operator provids a way to choose a defined value from a list of variables.
1212+ The result of ` a ?? b` is ` a` unless it' s `null / undefined`, them `b`.
1213+
1214+ - **Comparisons**
1215+
1216+ Equality check `==` for values of different types converts them to a number, so these are euqal:
1217+
1218+ ```JavaScript
1219+ alert( 0 == false ); // true
1220+ alert( 0 == ' ' ); // true
1221+ ```
1222+
1223+ Other comperisons convert to a number as well.
1224+
1225+ The strict equality operator `===` doesn' t do the conversion: different types always mean different values for it.
1226+ - Values ` null` and ` undefined` are special: they equal ` ==` each other and don' t euqal anything else.
1227+ - And `NaN != NaN` is true.
1228+ - `"" == 0` and `" " == 0` are also true.
1229+
1230+ Greater/less comparisons compare strings character-by-character, other types are converted to a number.
1231+
1232+ #### Loops
1233+
1234+ - 3 type of loops
1235+
1236+ ```JavaScript
1237+ // 1
1238+ while (condition) {
1239+ ...
1240+ }
1241+
1242+ // 2
1243+ do {
1244+ ...
1245+ } while (condition);
1246+
1247+ // 3
1248+ for (let i = 0; i < 10; i++) {
1249+ ...
1250+ }
1251+ ```
1252+
1253+ - Directives `break/continue` allow to exit the whole loop/current iteration. Use `label` to break nested loops.
1254+
1255+ #### The "switch" construct
1256+
1257+ The "switch" construct can replace multiple `if` checks.
1258+ It uses `===` (strict equality) for comparisons.
1259+
1260+ ```JavaScript
1261+ let age = prompt(' Your age? ' , 18);
1262+
1263+ switch (age) {
1264+ case 18:
1265+ alert("Won' t work" ); // the result of prompt is a string, not a number
1266+ break;
1267+
1268+ case " 18 " :
1269+ alert(" This works! " );
1270+ break;
1271+
1272+ default:
1273+ alert(" Any value not equal to one above" );
1274+ }
1275+ ```
1276+
1277+ #### Functions
1278+
1279+ Three ways to create a function in JavaScript:
1280+
1281+ 1. Function Declaration: the function in the main code
1282+
1283+ ```JavaScript
1284+ function sum(a, b) {
1285+ let result = a + b;
1286+ return result;
1287+ }
1288+ ```
1289+
1290+ 2. Function Expression: the function in the context of an expression
1291+
1292+ ```JavaScript
1293+ let sum = function(a, b) {
1294+ let result = a + b;
1295+ return result;
1296+ }
1297+ ```
1298+
1299+ 3. Arrow Functions:
1300+
1301+ ```JavaScript
1302+ // expression on the right side
1303+ let sum = (a, b) => a + b;
1304+
1305+ // multi line syntax { ... }
1306+ let sum = (a, b) => {
1307+ return a + b;
1308+ }
1309+
1310+ // without arguemnts
1311+ let sayHi = () => alert(" hello" );
1312+
1313+ // with a single argument
1314+ let double = n => n * 2;
1315+ ```
1316+
1317+ - Parameters can have default values: `function sum(a = 1, b = 2) {...}`
1318+
1319+ - Functions always return something. If there is no `return` statement, then return `undefined`.
0 commit comments