|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +title: Data Types in JS |
| 4 | +sidebar_label: Data Types |
| 5 | +description: "Learn the different types of data JavaScript can handle, from text to logic." |
| 6 | +--- |
| 7 | + |
| 8 | +JavaScript needs to know what kind of data it is working with so it knows what it can do with it. For example, you can multiply two **Numbers**, but you can't multiply two **Sentences**. |
| 9 | + |
| 10 | +There are 7 "Primitive" data types in JS, but as a beginner, you only need to master the **Big 5**. |
| 11 | + |
| 12 | +## 1. Strings (Text) |
| 13 | +A **String** is just plain text. It must always be wrapped in quotes: single `' '`, double `" "`, or backticks <code>` `</code>. |
| 14 | + |
| 15 | +* **Analogy:** Like a "string" of pearls, it is a sequence of characters linked together. |
| 16 | +* **Use for:** Names, addresses, or any messages. |
| 17 | + |
| 18 | +```javascript |
| 19 | +const name = "Ajay Dhangar"; |
| 20 | +const message = 'Welcome to CodeHarborHub'; |
| 21 | +``` |
| 22 | + |
| 23 | +## 2. Numbers (Math) |
| 24 | + |
| 25 | +Unlike many other languages, JS only has one type for numbers. They can be whole integers or decimals. **Do not use quotes** for numbers, or JS will think they are text! |
| 26 | + |
| 27 | +* **Use for:** Scores, prices, age, or calculations. |
| 28 | + |
| 29 | +```javascript |
| 30 | +const age = 25; |
| 31 | +const price = 9.99; |
| 32 | + |
| 33 | +// Comparison: |
| 34 | +const score = 10; // This is a Number (You can do math with this) |
| 35 | +const points = "10"; // This is a String (This is just a picture of a 10) |
| 36 | +``` |
| 37 | + |
| 38 | +## 3. Booleans (Logic) |
| 39 | + |
| 40 | +A Boolean can only be one of two values: `true` or `false`. |
| 41 | + |
| 42 | +* **Analogy:** Like a light switch. It is either **ON** or **OFF**. |
| 43 | +* **Use for:** Checking if a user is logged in, if a game is over, or if a checkbox is clicked. |
| 44 | + |
| 45 | +```javascript |
| 46 | +let isDarkMode = true; |
| 47 | +let isGameOver = false; |
| 48 | +``` |
| 49 | + |
| 50 | +## 4. Undefined (The Empty Box) |
| 51 | + |
| 52 | +When you create a variable but don't put anything in it yet, it is `undefined`. |
| 53 | + |
| 54 | +* **Analogy:** You bought a box (variable), but it's still empty and waiting for content. |
| 55 | + |
| 56 | +```javascript |
| 57 | +let mySecret; |
| 58 | +console.log(mySecret); // Result: undefined |
| 59 | +``` |
| 60 | + |
| 61 | +## 5. Null (The Intentional Nothing) |
| 62 | + |
| 63 | +`null` is a value that represents "nothing" or "empty" on purpose. |
| 64 | + |
| 65 | +* **The Difference:** `undefined` is an accidental empty box. `null` is you specifically saying, "This box is intentionally empty." |
| 66 | + |
| 67 | +```javascript |
| 68 | +let userAwards = null; // The user has zero awards right now |
| 69 | +``` |
| 70 | + |
| 71 | +## Interactive Lab: The "Typeof" Tool |
| 72 | + |
| 73 | +How do you check what is inside a variable? We use a special command called `typeof`. |
| 74 | + |
| 75 | +<CodePenEmbed |
| 76 | +title="JS Data Types Explorer" |
| 77 | +penId="bNwNbOE" |
| 78 | +/> |
| 79 | + |
| 80 | +### Challenge Tasks: |
| 81 | + |
| 82 | +1. In the JS tab, create a variable `let test = "5";`. |
| 83 | +2. Use `console.log(typeof test);`. Is it a number or a string? |
| 84 | +3. Remove the quotes around the `5`. Check the `typeof` again. See the difference? |
| 85 | + |
| 86 | +## Pro Tip: Template Literals |
| 87 | + |
| 88 | +When you want to mix a **String** with a **Variable**, use backticks ``` and the `${}` syntax. It’s much easier than using the `+` sign! |
| 89 | + |
| 90 | +```javascript title="index.js" |
| 91 | +const username = "Ajay"; |
| 92 | + |
| 93 | +// The old way (Hard) |
| 94 | +console.log("Hello " + username + "!"); |
| 95 | + |
| 96 | +// The modern way (Easy) |
| 97 | +console.log(`Hello ${username}!`); |
| 98 | +``` |
| 99 | + |
| 100 | +## Summary Checklist |
| 101 | + |
| 102 | +* [x] I know that **Strings** must be inside quotes. |
| 103 | +* [x] I understand that **Numbers** do not use quotes. |
| 104 | +* [x] I know that **Booleans** are for True/False logic. |
| 105 | +* [x] I can use `typeof` to identify a data type. |
| 106 | +* [x] I understand the difference between `null` and `undefined`. |
0 commit comments