Skip to content

Commit fea0579

Browse files
author
Muhammad Raza
committed
Added Chapter 5: Data Types in JavaScript
1 parent 403e0f3 commit fea0579

1 file changed

Lines changed: 183 additions & 0 deletions

File tree

06_Chapter-5/README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# Chapter 5: Data Types in JavaScript
2+
3+
## Introduction (English)
4+
5+
Welcome to **Chapter 5: Data Types in JavaScript**. In this chapter, we will explore different data types in JavaScript and understand how they work.
6+
7+
By the end of this chapter, you will:
8+
- Understand what **data types** are.
9+
- Learn the difference between **primitive** and **non-primitive** data types.
10+
- Know how to use different data types effectively.
11+
12+
---
13+
14+
## What are Data Types?
15+
Data types define the type of value stored in a variable. JavaScript has two main categories of data types:
16+
17+
1. **Primitive Data Types** (Stored directly in memory)
18+
2. **Non-Primitive (Reference) Data Types** (Stored by reference)
19+
20+
### 1. Primitive Data Types
21+
Primitive data types include:
22+
- `String` (Text values)
23+
- `Number` (Numeric values)
24+
- `Boolean` (True/False values)
25+
- `Null` (Empty value)
26+
- `Undefined` (Variable declared but not assigned)
27+
- `Symbol` (Unique identifiers, ES6+)
28+
- `BigInt` (Large numbers, ES11+)
29+
30+
#### Example 1: Primitive Data Types
31+
```javascript
32+
let name = "John"; // String
33+
let age = 25; // Number
34+
let isStudent = true; // Boolean
35+
let emptyValue = null; // Null
36+
let notAssigned; // Undefined
37+
let uniqueId = Symbol("id"); // Symbol
38+
let bigNumber = 123456789012345678901234567890n; // BigInt
39+
```
40+
41+
---
42+
43+
### 2. Non-Primitive (Reference) Data Types
44+
These include:
45+
- `Object` (Collection of key-value pairs)
46+
- `Array` (List of values)
47+
- `Function` (Reusable block of code)
48+
49+
#### Example 2: Non-Primitive Data Types
50+
```javascript
51+
let person = { name: "John", age: 25 }; // Object
52+
let numbers = [1, 2, 3, 4, 5]; // Array
53+
let greet = function() { console.log("Hello, World!"); }; // Function
54+
```
55+
56+
---
57+
58+
## Difference Between Primitive and Non-Primitive Data Types
59+
| Feature | Primitive | Non-Primitive |
60+
|---------|----------|--------------|
61+
| Stored in memory | Directly | By reference |
62+
| Mutability | Immutable | Mutable |
63+
| Example | `String`, `Number` | `Object`, `Array` |
64+
65+
---
66+
67+
## Best Practices for Using Data Types
68+
1. **Use `const` for constants** and `let` for variables that change.
69+
2. **Prefer `null` over `undefined`** when explicitly assigning an empty value.
70+
3. **Use objects for complex data** instead of multiple separate variables.
71+
4. **Use arrays for collections of values**.
72+
5. **Check data type using `typeof`**.
73+
74+
#### Example 3: Checking Data Type
75+
```javascript
76+
console.log(typeof "Hello"); // String
77+
console.log(typeof 100); // Number
78+
console.log(typeof true); // Boolean
79+
console.log(typeof {}); // Object
80+
console.log(typeof []); // Object (Array is a type of object)
81+
console.log(typeof function(){}); // Function
82+
```
83+
84+
---
85+
86+
## Assignments
87+
1. Declare a variable of each primitive data type and log its value.
88+
2. Create an object with at least three properties.
89+
3. Write a function and store it in a variable.
90+
4. Declare an array and access its elements using indexes.
91+
5. Use `typeof` to check the data type of different variables.
92+
93+
---
94+
95+
## Summary
96+
- JavaScript has **primitive** and **non-primitive** data types.
97+
- Primitive types include `String`, `Number`, `Boolean`, `Null`, `Undefined`, `Symbol`, and `BigInt`.
98+
- Non-primitive types include `Object`, `Array`, and `Function`.
99+
- **Primitive values are stored directly**, while **non-primitive values are stored by reference**.
100+
101+
This chapter is presented by **Muhammad Raza** (SMRIT - Smart Modern Revolutionary IT Training). 🚀
102+
103+
---
104+
105+
## ڈیٹا ٹائپس (Urdu)
106+
107+
### ڈیٹا ٹائپس کیا ہیں؟
108+
ڈیٹا ٹائپس وہ اقسام ہوتی ہیں جو کسی بھی ویری ایبل میں محفوظ ہونے والی ویلیو کی نوعیت کو ظاہر کرتی ہیں۔
109+
110+
### 1. بنیادی (Primitive) ڈیٹا ٹائپس
111+
یہ وہ ڈیٹا ٹائپس ہیں جو براہ راست میموری میں محفوظ ہوتی ہیں:
112+
- **String** (متنی اقدار)
113+
- **Number** (عددی اقدار)
114+
- **Boolean** (True/False)
115+
- **Null** (خالی ویلیو)
116+
- **Undefined** (متغیر متعین لیکن غیر مختص شدہ)
117+
- **Symbol** (منفرد شناخت کار)
118+
- **BigInt** (بہت بڑی عددی ویلیو)
119+
120+
#### مثال:
121+
```javascript
122+
let naam = "Ali"; // String
123+
let umar = 30; // Number
124+
let talib = false; // Boolean
125+
let khaali = null; // Null
126+
let mutaiyan; // Undefined
127+
let pehchan = Symbol("id"); // Symbol
128+
let baraAdad = 987654321012345678901234567890n; // BigInt
129+
```
130+
131+
---
132+
133+
### 2. غیر بنیادی (Non-Primitive) ڈیٹا ٹائپس
134+
یہ ریفرنس کے ذریعے محفوظ ہوتے ہیں:
135+
- **Object** (کلیدی جوڑوں کی کلیکشن)
136+
- **Array** (ویلیوز کی فہرست)
137+
- **Function** (قابل استعمال کوڈ بلاک)
138+
139+
#### مثال:
140+
```javascript
141+
let shakhs = { naam: "Ali", umar: 30 }; // Object
142+
let adad = [1, 2, 3, 4, 5]; // Array
143+
let salam = function() { console.log("سلام دنیا!"); }; // Function
144+
```
145+
146+
---
147+
148+
## بہترین طریقے
149+
1. **`const` مستقل اقدار کے لیے استعمال کریں**، `let` قابل تبدیلی متغیرات کے لیے۔
150+
2. **`null` کو `undefined` پر ترجیح دیں** جب کوئی خالی قدر متعین کرنی ہو۔
151+
3. **معقد ڈیٹا کے لیے objects استعمال کریں**۔
152+
4. **مجموعی ڈیٹا کے لیے arrays استعمال کریں**۔
153+
5. **`typeof` کا استعمال کرکے ڈیٹا کی قسم چیک کریں**۔
154+
155+
#### مثال:
156+
```javascript
157+
console.log(typeof "سلام"); // String
158+
console.log(typeof 50); // Number
159+
console.log(typeof true); // Boolean
160+
console.log(typeof {}); // Object
161+
console.log(typeof []); // Object (Array بھی ایک object کی قسم ہے)
162+
console.log(typeof function(){}); // Function
163+
```
164+
165+
---
166+
167+
## اسائنمنٹس
168+
1. ہر بنیادی ڈیٹا ٹائپ کے لیے ایک متغیر بنائیں اور اس کی ویلیو پرنٹ کریں۔
169+
2. تین پراپرٹیز کے ساتھ ایک object بنائیں۔
170+
3. ایک function لکھیں اور اسے ایک ویری ایبل میں محفوظ کریں۔
171+
4. ایک array بنائیں اور اس کے عناصر تک رسائی حاصل کریں۔
172+
5. مختلف متغیرات کی قسم `typeof` سے چیک کریں۔
173+
174+
---
175+
176+
## خلاصہ
177+
- جاوا اسکرپٹ میں **بنیادی اور غیر بنیادی** ڈیٹا ٹائپس موجود ہیں۔
178+
- بنیادی ٹائپس میں `String`, `Number`, `Boolean`, `Null`, `Undefined`, `Symbol`, اور `BigInt` شامل ہیں۔
179+
- غیر بنیادی ٹائپس میں `Object`, `Array`, اور `Function` شامل ہیں۔
180+
- **بنیادی اقدار براہ راست محفوظ ہوتی ہیں** جبکہ **غیر بنیادی اقدار ریفرنس کے ذریعے محفوظ ہوتی ہیں**۔
181+
182+
یہ باب **محمد رضا** (SMRIT - Smart Modern Revolutionary IT Training) کی جانب سے پیش کیا گیا ہے۔ 🚀
183+

0 commit comments

Comments
 (0)