Skip to content

Commit a2d3e8a

Browse files
Added foundational JavaScript concepts including code structure, strict mode, and script tag usage
1 parent 4831fa6 commit a2d3e8a

3 files changed

Lines changed: 384 additions & 0 deletions

File tree

01.01_Code_Structure.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# 🧱 JavaScript Code Structure
2+
3+
The building blocks of code.
4+
5+
## 📌 Statements
6+
7+
Statements are syntax constructs and commands that perform actions.
8+
9+
For example We’ve already seen a statement, `alert('Hello, world!')`, which shows the message “Hello, world!”.:
10+
11+
```js
12+
alert("Hello, world!");
13+
```
14+
15+
We can have as many statements in our code as we want. They can be separated with a semicolon (`;`):
16+
17+
```js
18+
alert("Hello"); alert("World");
19+
```
20+
21+
But usually, for readability, we write them on separate lines:
22+
23+
```js
24+
alert("Hello");
25+
alert("World");
26+
```
27+
28+
---
29+
30+
## 🟡 Semicolons
31+
32+
Semicolons can be **omitted** in many cases where a line break exists:
33+
34+
This would also work:
35+
36+
```js
37+
alert("Hello")
38+
alert("World")
39+
```
40+
41+
Here, JavaScript interprets the line break as an **implicit** semicolon. This is called **automatic semicolon insertion**. But be careful—**not all cases** are safe.
42+
43+
**In most cases, a newline implies a semicolon. But “in most cases” does not mean “always”!** There are cases when a newline does not mean a semicolon. For example:
44+
45+
Example:
46+
47+
```js
48+
alert(3 +
49+
1
50+
+ 2);
51+
```
52+
53+
✅ This works as expected and shows `6` because JavaScript does not insert semicolons here. JavaScript knows the line ends in `+`, so it continues.
54+
55+
However, **there are situations where JavaScript “fails” to assume a semicolon where it is really needed**:
56+
57+
```js
58+
alert("Hello");
59+
60+
[1, 2].forEach(alert);
61+
```
62+
63+
This shows:
64+
65+
```
66+
Hello
67+
1
68+
2
69+
```
70+
71+
But if you remove the semicolon after the `alert`:
72+
73+
```js
74+
alert("Hello")
75+
76+
[(1, 2)].forEach(alert);
77+
```
78+
79+
It fails!
80+
The difference compared to the code above is only one character: the semicolon at the end of the first line is gone.
81+
82+
If we run this code, only the first Hello shows (and there’s an error, you may need to open the console to see it). There are no numbers any more.
83+
84+
That’s because JavaScript does not assume a semicolon before square brackets [...]. So, the code in the last example is treated as a single statement.
85+
86+
Here’s how the engine sees it:
87+
88+
```js
89+
alert("Hello")[(1, 2)].forEach(alert);
90+
```
91+
92+
Which is incorrect. So, always using semicolons is a **safe practice**, especially for beginners.
93+
94+
---
95+
96+
## 💬 Comments
97+
98+
As code grows and becomes more and more complex, **comments** help explain what’s going on.
99+
100+
Comments can be put into any place of a script. They don’t affect its execution because the engine simply ignores them.
101+
102+
### ✅ Single-line comments
103+
104+
One-line comments start with two forward slash characters `//`:
105+
106+
The rest of the line is a comment. It may occupy a full line of its own or follow a statement.
107+
108+
```js
109+
// This comment is on its own line
110+
alert("Hello");
111+
112+
alert("World"); // This one follows a statement
113+
```
114+
115+
### ✅ Multiline comments
116+
117+
Multiline comments start with a forward slash and an asterisk `/* `and end with an asterisk and a forward slash `*/`.
118+
119+
```js
120+
/* This is a
121+
multiline comment */
122+
alert("Hello");
123+
alert("World");
124+
```
125+
126+
You can also temporarily disable code:
127+
128+
```js
129+
/*
130+
alert('Hello');
131+
*/
132+
alert("World");
133+
```
134+
135+
### ⚠️ No Nested Comments!
136+
137+
There may not be `/*...*/` inside another `/*...*/`.
138+
139+
Such code will die with an error:
140+
141+
```js
142+
/*
143+
/* Nested comment? */
144+
*/
145+
alert('World');
146+
```
147+
148+
---
149+
150+
## ⚡ Editor Tip: Use Hotkeys!
151+
152+
- Single-line comment: `Ctrl + /` (or `Cmd + /` on Mac)
153+
- Multiline comment: `Ctrl + Shift + /` (or `Cmd + Option + /` on Mac)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# 🛡️ JavaScript Strict Mode – `"use strict"`
2+
3+
## 📜 What is `"use strict"`?
4+
5+
JavaScript has evolved over time while keeping old code working. This meant that early mistakes in the language couldn’t be fixed without breaking existing programs.
6+
7+
That changed in **2009**, with the release of **ECMAScript 5 (ES5)**. It introduced `"use strict"` – a way to opt in to a safer, modern version of JavaScript.
8+
9+
---
10+
11+
## 🧪 Enabling Strict Mode
12+
13+
Strict mode is enabled by placing `"use strict"` at the **very top** of your script or function:
14+
15+
```js
16+
"use strict";
17+
18+
// Your modern JavaScript code here
19+
```
20+
21+
You can also enable it inside a function:
22+
23+
```js
24+
function example() {
25+
"use strict";
26+
// strict mode only applies inside this function
27+
}
28+
```
29+
30+
---
31+
32+
## 🚨 Strict Mode Must Be at the Top
33+
34+
Strict mode won’t work if it’s **not at the top**:
35+
36+
```js
37+
alert("Some code");
38+
("use strict"); // ❌ Ignored!
39+
```
40+
41+
✅ Only comments may appear before `"use strict"`.
42+
43+
---
44+
45+
## ❌ No Way to Cancel
46+
47+
Once strict mode is enabled, you **cannot disable it**. There’s no `"no strict"` or similar directive.
48+
49+
---
50+
51+
## 🧪 Using Strict in the Browser Console
52+
53+
Browser consoles don’t run in strict mode by default.
54+
55+
### ✅ To enable it manually:
56+
57+
```js
58+
"use strict";
59+
// your code here
60+
```
61+
62+
Use **Shift + Enter** to input multiline code before running it.
63+
64+
### 💡 Compatibility Wrapper:
65+
66+
If your console doesn’t behave well, wrap your code in an **IIFE** (Immediately Invoked Function Expression):
67+
68+
```js
69+
(function () {
70+
"use strict";
71+
// your code here
72+
})();
73+
```
74+
75+
---
76+
77+
## 🤔 Should You Use Strict Mode?
78+
79+
Yes – at least **for now**.
80+
81+
Strict mode helps catch bugs, improves performance in some cases, and prevents certain bad practices.
82+
83+
```js
84+
"use strict";
85+
// safer and cleaner code
86+
```
87+
88+
But… when you start using **classes** or **modules**, strict mode is enabled automatically. So:
89+
90+
✅ Use `"use strict"` now.
91+
92+
💡 Later, with **modern features**, you won’t need it!
93+
94+
---
95+
96+
### 🔐 Benefits of `"use strict"`:
97+
98+
- Makes it easier to write secure JavaScript.
99+
- Prevents the use of undeclared variables.
100+
- Eliminates silent errors.
101+
- Fixes mistakes in the language.
102+
103+
---
104+
105+
Stay strict, code safe! 💻🔒

01_JavaScript_Fundamentals.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# 👋 Hello, World!
2+
3+
## 🧩 The `<script>` Tag
4+
5+
JavaScript can be embedded almost anywhere in an HTML document using the `<script>` tag.
6+
7+
Here’s a basic example:
8+
9+
```html
10+
<!DOCTYPE html>
11+
<html>
12+
<body>
13+
<p>Before the script...</p>
14+
15+
<script>
16+
alert("Hello, world!");
17+
</script>
18+
19+
<p>...After the script.</p>
20+
</body>
21+
</html>
22+
```
23+
24+
📌 The code inside the `<script>` tag is automatically executed when the browser processes it.
25+
26+
---
27+
28+
## 📜 A Bit of Script History
29+
30+
The `<script>` tag has a few attributes that are rarely used nowadays but can still be found in old code:
31+
32+
### 🔹 `type` Attribute
33+
34+
```html
35+
<script type="text/javascript">
36+
```
37+
38+
- In **HTML4**, the `type` attribute was required.
39+
- Typically set as `type="text/javascript"`.
40+
- In **modern HTML**, it’s **optional**.
41+
- Today, it’s used for JavaScript **modules**, like `type="module"`, but we’ll cover that later.
42+
43+
### 🔹 `language` Attribute
44+
45+
```html
46+
<script language="JavaScript">
47+
```
48+
49+
- This was meant to specify the script’s language.
50+
- It's now **deprecated**—JavaScript is the **default**.
51+
- You **don’t need to use it** anymore.
52+
53+
### 🔹 Old-Style Comments
54+
55+
In really old tutorials, you might see something like:
56+
57+
```html
58+
<script type="text/javascript">
59+
<!--
60+
alert("Hello, old world!");
61+
//-->
62+
</script>
63+
```
64+
65+
- These were used to **hide JS from old browsers**.
66+
- Totally unnecessary today.
67+
- Can be a sign of **very old code**.
68+
69+
---
70+
71+
## 📁 External Scripts
72+
73+
If your JS code is large or reused, it’s best to keep it in a separate file.
74+
75+
### ✅ Basic Usage
76+
77+
```html
78+
<script src="/path/to/script.js"></script>
79+
```
80+
81+
- The path can be **absolute** (`/path/to/`) or **relative** (`./script.js`).
82+
- You can also use a full URL:
83+
84+
```html
85+
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
86+
```
87+
88+
### 🔗 Multiple Scripts
89+
90+
To include several external scripts, use multiple `<script>` tags:
91+
92+
```html
93+
<script src="/js/script1.js"></script>
94+
<script src="/js/script2.js"></script>
95+
```
96+
97+
---
98+
99+
## ⚠️ Important Notes
100+
101+
- Only **simple scripts** are written directly in HTML.
102+
- More complex or reusable code goes in **external files**.
103+
- Browsers **cache external files**, which improves performance and reduces bandwidth usage.
104+
105+
---
106+
107+
## ❌ Don’t Mix `src` with Inline Code
108+
109+
This **won’t work**:
110+
111+
```html
112+
<script src="file.js">
113+
alert(1); // ❌ ignored!
114+
</script>
115+
```
116+
117+
Use separate `<script>` tags instead:
118+
119+
```html
120+
<script src="file.js"></script>
121+
<script>
122+
alert(1); // ✅ works
123+
</script>
124+
```
125+
126+
---

0 commit comments

Comments
 (0)