Skip to content

Commit 3dfc5a2

Browse files
committed
post: complete JS Fundments
1 parent 750e587 commit 3dfc5a2

File tree

7 files changed

+398
-13
lines changed

7 files changed

+398
-13
lines changed

content/posts/2025-09-21_morden-js-fundamentals-03.md

Lines changed: 148 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,10 @@ function checkAge(age) {
824824
```
825825
826826
Rewrite 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
832833
function checkAge(age) {
@@ -1172,3 +1173,147 @@ let isTeaWanted = confirm("Do u want tea?");
11721173
alert( "Visitor: " + userName );
11731174
alert( "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`.

public/archives/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<span>14
6767
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/morden-javascript-tutorial-chapter-2-fundamentals-11~18/ class=block>Morden Javascript Tutorial Chapter 2 - Fundamentals: 11~18</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
6868
<time datetime=2025-09-21>09-21</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
69-
<span>11
69+
<span>12
7070
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/english-for-programmers-03/ class=block>English for Programmers - 03</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
7171
<time datetime=2025-09-20>09-20</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
7272
<span>1

0 commit comments

Comments
 (0)