|
1 |
| -## JavaScript Cheatsheet |
| 1 | +# JavaScript Cheatsheet |
2 | 2 |
|
3 |
| -This cheatsheet covers essential **JavaScript** concepts, providing a quick reference guide for beginners who want to master JavaScript fundamentals. |
| 3 | +## 1. Variables |
| 4 | +- **`let`**: Declares a block-scoped variable. |
| 5 | + ```js |
| 6 | + let x = 5; |
| 7 | + ``` |
| 8 | +- **`const`**: Declares a constant block-scoped variable. |
| 9 | + ```js |
| 10 | + const pi = 3.14; |
| 11 | + ``` |
| 12 | +- **`var`**: Declares a function-scoped variable (legacy). |
| 13 | + ```js |
| 14 | + var name = "Dev"; |
| 15 | + ``` |
4 | 16 |
|
5 |
| -## Learning Objectives |
6 |
| -- Understand JavaScript syntax and data types. |
7 |
| -- Work with variables and functions. |
8 |
| -- Learn about JavaScript's scope and closures. |
9 |
| -- Understand how to manipulate the DOM. |
10 |
| -- Explore asynchronous programming using promises and `async/await`. |
11 |
| -- Learn about ES6+ features like arrow functions, destructuring, and modules. |
12 |
| -- Implement object-oriented programming in JavaScript. |
13 |
| -- Utilize common array methods for data manipulation. |
| 17 | +## 2. Data Types |
| 18 | +- **String**: |
| 19 | + ```js |
| 20 | + let str = "Hello, World!"; |
| 21 | + ``` |
| 22 | +- **Number**: |
| 23 | + ```js |
| 24 | + let num = 42; |
| 25 | + ``` |
| 26 | +- **Boolean**: |
| 27 | + ```js |
| 28 | + let isJavaScriptFun = true; |
| 29 | + ``` |
| 30 | +- **Array**: |
| 31 | + ```js |
| 32 | + let arr = [1, 2, 3, 4]; |
| 33 | + ``` |
14 | 34 |
|
15 |
| -## Cheatsheet Outline |
| 35 | +- **Object**: |
| 36 | + ```js |
| 37 | + let person = { |
| 38 | + name: "Dev", |
| 39 | + age: 25 |
| 40 | + }; |
| 41 | + ``` |
16 | 42 |
|
17 |
| -### Module 1: JavaScript Basics |
18 |
| -- Overview of JavaScript and its role in web development. |
19 |
| -- Data types and variables (`var`, `let`, `const`). |
| 43 | +## 3. Functions |
| 44 | +- **Function Declaration**: |
| 45 | + ```js |
| 46 | + function greet(name) { |
| 47 | + return "Hello, " + name; |
| 48 | + } |
| 49 | + ``` |
20 | 50 |
|
21 |
| -### Module 2: Functions and Scope |
22 |
| -- Creating functions and understanding their scope. |
23 |
| -- Arrow functions and their usage. |
| 51 | +- **Arrow Function**: |
| 52 | + ```js |
| 53 | + const greet = (name) => `Hello, ${name}`; |
| 54 | + ``` |
24 | 55 |
|
25 |
| -### Module 3: Control Flow |
26 |
| -- Using `if`, `else`, `switch`, and loops (`for`, `while`). |
| 56 | +## 4. Conditionals |
| 57 | +- **If-Else Statement**: |
| 58 | + ```js |
| 59 | + if (x > 10) { |
| 60 | + console.log("x is greater than 10"); |
| 61 | + } else { |
| 62 | + console.log("x is less than or equal to 10"); |
| 63 | + } |
| 64 | + ``` |
27 | 65 |
|
28 |
| -### Module 4: DOM Manipulation |
29 |
| -- Selecting and modifying HTML elements using JavaScript. |
30 |
| -- Understanding `querySelector` and `getElementById`. |
| 66 | +- **Switch Statement**: |
| 67 | + ```js |
| 68 | + switch (day) { |
| 69 | + case "Monday": |
| 70 | + console.log("It's Monday!"); |
| 71 | + break; |
| 72 | + default: |
| 73 | + console.log("It's not Monday."); |
| 74 | + } |
| 75 | + ``` |
31 | 76 |
|
32 |
| -### Module 5: Asynchronous JavaScript |
33 |
| -- Introduction to callbacks, promises, and `async/await`. |
34 |
| -- Using `fetch` for making HTTP requests. |
| 77 | +## 5. Loops |
| 78 | +- **For Loop**: |
| 79 | + ```js |
| 80 | + for (let i = 0; i < 5; i++) { |
| 81 | + console.log(i); |
| 82 | + } |
| 83 | + ``` |
35 | 84 |
|
36 |
| -### Module 6: ES6+ Features |
37 |
| -- Using `let` and `const` for variable declarations. |
38 |
| -- Object and array destructuring. |
39 |
| -- Import/export modules. |
| 85 | +- **While Loop**: |
| 86 | + ```js |
| 87 | + let i = 0; |
| 88 | + while (i < 5) { |
| 89 | + console.log(i); |
| 90 | + i++; |
| 91 | + } |
| 92 | + ``` |
40 | 93 |
|
41 |
| -### Module 7: Object-Oriented JavaScript |
42 |
| -- Understanding objects, classes, and inheritance. |
43 |
| -- Creating and using classes in JavaScript. |
| 94 | +- **ForEach Loop (for arrays)**: |
| 95 | + ```js |
| 96 | + const arr = [1, 2, 3, 4]; |
| 97 | + arr.forEach(item => console.log(item)); |
| 98 | + ``` |
44 | 99 |
|
45 |
| -### Module 8: Array Methods |
46 |
| -- Overview of common array methods like `map`, `filter`, `reduce`, and `forEach`. |
47 |
| -- Examples of using these methods for data transformation. |
| 100 | +## 6. Arrays and Methods |
| 101 | +- **Push and Pop**: |
| 102 | + ```js |
| 103 | + let fruits = ["apple", "banana"]; |
| 104 | + fruits.push("orange"); // ["apple", "banana", "orange"] |
| 105 | + fruits.pop(); // ["apple", "banana"] |
| 106 | + ``` |
48 | 107 |
|
49 |
| -## Prerequisites |
50 |
| -- Basic understanding of HTML and CSS. |
51 |
| -- No prior programming experience is required. |
| 108 | +- **Map, Filter, and Reduce**: |
| 109 | + ```js |
| 110 | + let numbers = [1, 2, 3, 4]; |
| 111 | + let doubled = numbers.map(num => num * 2); // [2, 4, 6, 8] |
| 112 | + let even = numbers.filter(num => num % 2 === 0); // [2, 4] |
| 113 | + let sum = numbers.reduce((total, num) => total + num, 0); // 10 |
| 114 | + ``` |
52 | 115 |
|
53 |
| -## Conclusion |
54 |
| -By the end of this cheatsheet, users will have a strong grasp of **JavaScript** fundamentals and be ready to build dynamic, interactive web applications. |
| 116 | +## 7. Object Methods |
| 117 | +- **Accessing Object Properties**: |
| 118 | + ```js |
| 119 | + let person = { |
| 120 | + name: "Dev", |
| 121 | + age: 25 |
| 122 | + }; |
| 123 | + console.log(person.name); // "Dev" |
| 124 | + ``` |
| 125 | + |
| 126 | +- **Adding Methods to Objects**: |
| 127 | + ```js |
| 128 | + let car = { |
| 129 | + make: "Tesla", |
| 130 | + model: "Model 3", |
| 131 | + start: function() { |
| 132 | + console.log("Car started"); |
| 133 | + } |
| 134 | + }; |
| 135 | + car.start(); // "Car started" |
| 136 | + ``` |
| 137 | + |
| 138 | +## 8. Events (in a browser) |
| 139 | +- **Handling Click Events**: |
| 140 | + ```html |
| 141 | + <button onclick="alert('Button clicked!')">Click me</button> |
| 142 | + ``` |
| 143 | + |
| 144 | +- **Adding Event Listeners**: |
| 145 | + ```js |
| 146 | + document.querySelector("button").addEventListener("click", function() { |
| 147 | + alert("Button clicked!"); |
| 148 | + }); |
| 149 | + ``` |
| 150 | + |
| 151 | +## 9. Promises and Async-Await |
| 152 | +- **Promise**: |
| 153 | + ```js |
| 154 | + let promise = new Promise(function(resolve, reject) { |
| 155 | + let success = true; |
| 156 | + if (success) { |
| 157 | + resolve("Promise fulfilled!"); |
| 158 | + } else { |
| 159 | + reject("Promise rejected."); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + promise.then(function(message) { |
| 164 | + console.log(message); |
| 165 | + }); |
| 166 | + ``` |
| 167 | + |
| 168 | +- **Async-Await**: |
| 169 | + ```js |
| 170 | + async function fetchData() { |
| 171 | + let response = await fetch('https://api.example.com/data'); |
| 172 | + let data = await response.json(); |
| 173 | + console.log(data); |
| 174 | + } |
| 175 | + ``` |
| 176 | + |
| 177 | +## 10. DOM Manipulation |
| 178 | +- **Selecting Elements**: |
| 179 | + ```js |
| 180 | + let element = document.getElementById('myElement'); |
| 181 | + let elements = document.querySelectorAll('.myClass'); |
| 182 | + ``` |
| 183 | + |
| 184 | +- **Changing Content**: |
| 185 | + ```js |
| 186 | + element.innerHTML = "New content!"; |
| 187 | + ``` |
| 188 | + |
| 189 | +- **Changing Styles**: |
| 190 | + ```js |
| 191 | + element.style.color = "blue"; |
| 192 | + ``` |
0 commit comments