├── BOM.md ├── Basic-Operators.md ├── Conditional-Statements.md ├── DOM.md ├── Data-Types.md ├── Functions.md ├── Hello-world.md ├── JS-Comments.md ├── JavaScript-Engine.md ├── Logical-Operators.md ├── Loops.md ├── Nullish-Coalescing.md ├── README.md ├── Switch-Case.md ├── Type-Conversions.md ├── User-Interaction.md ├── Variables.md ├── images ├── DOM1.png ├── DOM2.png ├── DOM3.png ├── DOM4.png ├── DOM5.png ├── DOM6.png └── DOM7.gif └── what-is-JavaScript.md /BOM.md: -------------------------------------------------------------------------------- 1 | # JavaScript - Browser Object Model 2 | 3 | ## Browser Object Model in JavaScript 4 | 5 | The Browser Object Model (BOM) in JavaScript helps to interact with the browser, not just the webpage. While the DOM handles the content of the page, BOM gives you control over things like the browser window, the URL, and the Cookies. 6 | 7 | 8 | ### `window.location` in JavaScript** 9 | 10 | The `window.location` object in JavaScript provides details about the current page URL and allows you to modify it. It’s commonly used for getting URL information, redirecting users, or reloading the page. 11 | 12 | --- 13 | 14 | ### **Main Properties of `window.location`** 15 | 16 | Here’s a breakdown of its key properties: 17 | 18 | |Property|Description|Example Output| 19 | |---|---|---| 20 | |`window.location.href`|The full URL of the page|`"https://example.com/page.html?search=query"`| 21 | |`window.location.protocol`|The protocol used (`http:` or `https:`)|`"https:"`| 22 | |`window.location.host`|The domain name and port (if any)|`"example.com"` or `"localhost:3000"`| 23 | |`window.location.hostname`|The domain name only (without port)|`"example.com"`| 24 | |`window.location.port`|The port number (if any)|`"3000"` (if running on `localhost:3000`)| 25 | |`window.location.pathname`|The path after the domain|`"/page.html"`| 26 | |`window.location.search`|The query string (after `?`)|`"?search=query"`| 27 | |`window.location.hash`|The fragment identifier (after `#`)|`"#section1"`| 28 | |`window.location.origin`|The full URL origin (protocol + hostname + port)|`"https://example.com:8080"`| 29 | 30 | --- 31 | 32 | ### **Example: Getting URL Information** 33 | 34 | ```js 35 | console.log("Full URL:", window.location.href); 36 | console.log("Protocol:", window.location.protocol); 37 | console.log("Host:", window.location.host); 38 | console.log("Hostname:", window.location.hostname); 39 | console.log("Port:", window.location.port); 40 | console.log("Pathname:", window.location.pathname); 41 | console.log("Query String:", window.location.search); 42 | console.log("Hash:", window.location.hash); 43 | console.log("Origin:", window.location.origin); 44 | 45 | ``` 46 | 47 | **If the page URL is:** 48 | `https://example.com:8080/page.html?search=bug#results` 49 | This code will output: 50 | 51 | ``` 52 | Full URL: https://example.com:8080/page.html?search=bug#results 53 | Protocol: https: 54 | Host: example.com:8080 55 | Hostname: example.com 56 | Port: 8080 57 | Pathname: /page.html 58 | Query String: ?search=bug 59 | Hash: #results 60 | Origin: https://example.com:8080 61 | 62 | ``` 63 | 64 | --- 65 | 66 | ### **Redirecting & Reloading the Page** 67 | 68 | You can use `window.location` to navigate or refresh the page dynamically. 69 | 70 | #### **1. Redirect to Another Page** 71 | `window.location.href = "https://google.com";` 72 | Acts like clicking a link. 73 | 74 | #### **2. Reload the Page** 75 | `window.location.reload();` 76 | Equivalent to pressing **F5**. 77 | 78 | #### **3. Navigate Without Keeping History** 79 | `window.location.replace("https://example.com");` 80 | Unlike `href`, this removes the current page from history, so the user **can’t go back**. 81 | 82 | --- 83 | 84 | ### **Modifying URL Parts Dynamically** 85 | 86 | You can change specific parts of the URL. 87 | #### **1. Update the Query String** 88 | `window.location.search = "?user=Islam&role=admin";` 89 | Changes the query parameters. 90 | 91 | #### **2. Change the Hash** 92 | `window.location.hash = "#section3";` 93 | Scrolls to `#section3` **without reloading**. 94 | 95 | --- 96 | 97 | #### **Example: Redirect Based on User Choice** 98 | ```html 99 | 100 | 101 | 102 | 111 | 112 | ``` 113 | clicking a button redirects the user to a different page. 114 | 115 | 116 | --- 117 | ## JavaScript Cookies 118 | 119 | Cookies are small pieces of data stored in the browser, mainly used for tracking user sessions, storing preferences, and authentication. JavaScript allows you to **create, read, and delete** cookies using the `document.cookie` property. 120 | 121 | ### **How Cookies Work** 122 | 123 | A cookie is a simple key-value pair stored in the browser. Here’s an example: 124 | ``` 125 | username=Islam; expires=Fri, 31 Dec 2025 23:59:59 GMT; path=/ 126 | ``` 127 | - `username=Islam` → Stores the user’s name. 128 | - `expires=...` → Sets the expiration date. 129 | - `path=/` → Cookie is accessible across the whole website. 130 | 131 | 132 | ### **Creating a Cookie** 133 | 134 | You can set a cookie using `document.cookie` like this: 135 | ```js 136 | document.cookie = "username=Islam"; 137 | 138 | ``` 139 | 140 | This creates a basic cookie **without an expiration date**, meaning it will be deleted when the browser closes (a **session cookie**). 141 | 142 | #### **Setting an Expiration Date** 143 | 144 | To make a cookie persist, you must add an `expires` attribute. 145 | ```js 146 | document.cookie = "username=Islam; expires=Fri, 31 Dec 2025 23:59:59 GMT"; 147 | 148 | ``` 149 | This cookie will **stay** in the browser until **December 31, 2025**. 150 | 151 | #### **Setting a Path** 152 | 153 | If you want the cookie to be accessible across all pages, add `path=/`: 154 | ```js 155 | document.cookie = "username=Islam; expires=Fri, 31 Dec 2025 23:59:59 GMT; path=/"; 156 | 157 | ``` 158 | Without `path=/`, the cookie is **only available on the current page**. 159 | 160 | ### **Reading Cookies** 161 | 162 | You can retrieve all cookies using: 163 | ```js 164 | console.log(document.cookie); 165 | 166 | ``` 167 | This returns a string of all cookies: 168 | ``` 169 | "username=Islam; theme=dark; language=en" 170 | 171 | ``` 172 | Since cookies are stored as a **single string**, you may need to parse them manually. 173 | 174 | #### **Extracting a Specific Cookie** 175 | 176 | Example: Get the `username` cookie. 177 | ```js 178 | function getCookie(name) { 179 | let cookies = document.cookie.split("; "); 180 | for (let i = 0; i < cookies.length; i++) { 181 | let [key, value] = cookies[i].split("="); 182 | if (key === name) { 183 | return value; 184 | } 185 | } 186 | return null; 187 | } 188 | 189 | console.log(getCookie("username")); // Output: Islam 190 | 191 | ``` 192 | 193 | 194 | ### **Updating a Cookie** 195 | 196 | To update a cookie, you **set it again** with the same name but a new value. 197 | ```js 198 | document.cookie = "username=Matr; expires=Fri, 31 Dec 2025 23:59:59 GMT; path=/"; 199 | 200 | ``` 201 | The `username` cookie is now `"Matr"`. 202 | 203 | -------------------------------------------------------------------------------- /Basic-Operators.md: -------------------------------------------------------------------------------- 1 | ### Arithmetic Operators 2 | 3 | | Operator | Name | Purpose | Example | 4 | | -------- | ----------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- | 5 | | `+` | Addition | Adds two numbers together. | `6 + 9` | 6 | | `-` | Subtraction | Subtracts the right number from the left. | `20 - 15` | 7 | | `*` | Multiplication | Multiplies two numbers together. | `3 * 7` | 8 | | `/` | Division | Divides the left number by the right. | `10 / 5` | 9 | | `%` | Remainder (sometimes called modulo) | Returns the remainder left over after you've divided the left number into a number of integer portions equal to the right number. | `8 % 3` (returns 2, as three goes into 8 twice, leaving 2 left over). | 10 | | `**` | Exponent | Raises a `base` number to the `exponent` power, that is, the `base` number multiplied by itself, `exponent` times. | `5 ** 2` (returns `25`, which is the same as `5 * 5`). | 11 | | `++` | Increment | Increases the variable's number by 1. | let x=20; x++; x=21 | 12 | | `--` | Decrement | Decreases the variable's number by 1 | let x=20;x--;x=19 | 13 | 14 | ### Assignment Operators 15 | 16 | | Operator | Example | Same As | 17 | | -------- | ------- | ---------- | 18 | | = | x = y | x = y | 19 | | += | x += y | x = x + y | 20 | | -= | x -= y | x = x - y | 21 | | *= | x *= y | x = x * y | 22 | | /= | x /= y | x = x / y | 23 | | %= | x %= y | x = x % y | 24 | | **= | x **= y | x = x ** y | 25 | 26 | -------------------------------------------------------------------------------- /Conditional-Statements.md: -------------------------------------------------------------------------------- 1 | ### Conditional statements: if, else, ternary operator (?). 2 | 3 | JavaScript conditional statements allow you to execute specific blocks of code based on conditions. If the condition is met, a particular block of code will run; otherwise, another block of code will execute based on the condition. 4 | 5 | ##### 1. Using if Statement 6 | The if statement is used to evaluate a particular condition. If the condition holds true, the associated code block is executed. 7 | 8 | ``` 9 | let x = 20; 10 | 11 | if (x % 2 === 0) { 12 | console.log("Even"); 13 | } 14 | 15 | if (x % 2 !== 0) { 16 | console.log("Odd"); 17 | }; 18 | ``` 19 | 20 | --- 21 | ##### 2. Using if-else Statement 22 | Use `else` to specify a block of code to be executed, if the same condition is false 23 | 24 | ``` 25 | let age = 25; 26 | 27 | if (age >= 18) { 28 | console.log("Adult") 29 | } else { 30 | console.log("Not an Adult") 31 | }; 32 | ``` 33 | 34 | --- 35 | ##### 3. else if Statement 36 | Use `else if` to specify a new condition to test, if the first condition is false 37 | 38 | ``` 39 | if (time < 10) { 40 | greeting = "Good morning"; 41 | } else if (time < 20) { 42 | greeting = "Good day"; 43 | } else { 44 | greeting = "Good evening"; 45 | } 46 | // the result is Good evening 47 | ``` 48 | --- 49 | 50 | ##### 4. JavaScript Ternary Operator 51 | The Ternary Operator in JavaScript is a shortcut for writing simple if-else statements. It’s also known as the Conditional Operator because it works based on a condition. The ternary operator allows you to quickly decide between two values depending on whether a condition is true or false. 52 | 53 | ##### How Does the Ternary Operator Work? 54 | The ternary operator works with three parts: 55 | - ****Condition:**** A statement that returns true or false. 56 | - ****Value if True:**** What happens if the condition is true? 57 | - ****Value if False:**** What happens if the condition is false? 58 | 59 | ****Syntax:**** 60 | condition ? trueExpression : falseExpression 61 | 62 | Example --> 63 | 64 | ``` 65 | let age = 60; 66 | 67 | let res = (age > 59) ? "Adult" : "Not Adult"; 68 | 69 | console.log(res); 70 | // res --> Adult 71 | ``` 72 | 73 | if we want to convert it to normal if condition will be like that --> 74 | ``` 75 | let age = 60; 76 | let res; 77 | 78 | if (age > 59) { 79 | res = "Adult"; 80 | } else { 81 | res = "Not Adult"; 82 | } 83 | console.log(res); 84 | // res --> Adult 85 | ``` 86 | 87 | ##### Nested Ternary Operators 88 | multiple Conditional operators 89 | 90 | ``` 91 | let marks = 95; 92 | let res = (marks < 40) ? "Unsatisfactory" : 93 | (marks < 60) ? "Average" : 94 | (marks < 80) ? "Good" : "Excellent"; 95 | 96 | console.log(res); 97 | 98 | // res --> Excellent 99 | ``` 100 | 101 | -------------------------------------------------------------------------------- /DOM.md: -------------------------------------------------------------------------------- 1 | 2 | ### **What is the DOM?** 3 | 4 | The Document Object Model (DOM) is a programming interface for web documents. In simpler terms, it's like an organized map that represents the structure and content of a webpage. Think of it as a tree-like structure where each part of a webpage – like headings, paragraphs, images, buttons, and more – is a node. This tree of nodes is what allows JavaScript to access, modify, and manipulate the content of a webpage. 5 | 6 | Let's take a look at this HTML code to better understand the DOM tree structure. 7 | ``` 8 | 9 | 10 |
11 | 12 |This is a simple HTML example.
17 | 18 | 19 | ``` 20 | 21 | From a DOM perspective, each HTML element becomes a node in the DOM tree: 22 | 23 | - `html` is the root node. 24 | - `head` and `body` are child nodes of `html`. 25 | - `meta` and `title` are child nodes of `head`. 26 | - `h1` and `p` are child nodes of `body`. 27 | 28 | Here's an alternative approach to visually represent this hierarchy of nodes. 29 | 30 | [DOM Structure](images/DOM1.png) 31 | 32 | ### **Understanding** `window` and `document` 33 | 34 | Before we dive into the practical examples, let's meet two important players: `window` and `document`. 35 | 36 | - `window`: This is like the boss of the browser window. It represents the entire browser window and has control over things like opening new tabs, resizing the window, and more. 37 | - `document`: This is like the page itself. It's an object that holds all the HTML elements on your page. When we say "DOM," we often mean the `document` object. 38 | 39 | ### **Finding Elements: Your Webpage's Treasure Hunt** 40 | 41 | To truly grasp the magic of the DOM, let's start with a hands-on example. Imagine you have the following HTML structure: 42 | ```HTML 43 | 44 | 45 | 46 |Find Me!
52 |And Me Too!
53 |