html
html
1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html>
<head>
<title>Differences b/w Prefix and Postfix counter in JS | Makzo</title>
</head>
<body>
</body>
</html>
Enter to Rename, Shift+Enter to Preview
css
css
1
2
3
body {
}
Enter to Rename, Shift+Enter to Preview
js
js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* Differences between
* Prefix and Postfix counter in JS
* Written by Makzo
*/
// I'll explain it by using simple words
// POST-FIX (e.g. x++)
let x = 2;
alert(x++); // alerts 2
/* Why?
* ..because:
* the postfix counter (x++) shows you
* the original value of x before increasing its value. See:
x was 2. You later said x++;
x will now become 3. Yes that's correct...but instead of showing you that 3 which is the new value, it will show you the old value which is 2.
*/
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run