|
| 1 | +# Chapter 3: The Modern Mode - "use strict" |
| 2 | + |
| 3 | +## Introduction (English) |
| 4 | + |
| 5 | +Welcome to **Chapter 3: The Modern Mode - "use strict"**. In this chapter, we will learn about JavaScript's **strict mode**, why it is important, and how it helps us write safer and cleaner code. |
| 6 | + |
| 7 | +By the end of this chapter, you will: |
| 8 | +- Understand what **"use strict"** is. |
| 9 | +- Learn how to enable strict mode. |
| 10 | +- Identify the errors that strict mode prevents. |
| 11 | +- Write safer JavaScript code with strict mode. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## What is "use strict"? |
| 16 | +JavaScript **strict mode** is a way to enforce stricter parsing and error handling in your code. It helps avoid common coding mistakes and makes debugging easier. |
| 17 | + |
| 18 | +### How to Enable Strict Mode |
| 19 | +Strict mode is enabled by adding **"use strict"** at the beginning of a script. |
| 20 | + |
| 21 | +#### Example 1: Enabling Strict Mode in a Script |
| 22 | +```javascript |
| 23 | +"use strict"; |
| 24 | + |
| 25 | +let name = "John"; |
| 26 | +alert(name); |
| 27 | +``` |
| 28 | + |
| 29 | +#### Example 2: Strict Mode Preventing Errors |
| 30 | +```javascript |
| 31 | +"use strict"; |
| 32 | + |
| 33 | +let age = 25; |
| 34 | +alert(age); |
| 35 | +``` |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## Benefits of Strict Mode |
| 40 | +Strict mode helps in: |
| 41 | +1. **Preventing accidental global variables** |
| 42 | + ```javascript |
| 43 | + "use strict"; |
| 44 | + let x = 10; |
| 45 | + alert(x); |
| 46 | + ``` |
| 47 | + |
| 48 | +2. **Catching silent errors** |
| 49 | + ```javascript |
| 50 | + "use strict"; |
| 51 | + let y = "Hello"; |
| 52 | + alert(y); |
| 53 | + ``` |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## Assignments |
| 58 | +1. Enable **strict mode** in a JavaScript program and test it using `alert`. |
| 59 | +2. Write a program where strict mode **prevents accidental global variables** and display values using `alert`. |
| 60 | +3. Create a script that shows the **difference between strict mode and non-strict mode** using `alert`. |
| 61 | +4. Write a JavaScript script using `let` and `alert` to demonstrate strict mode errors. |
| 62 | +5. Use strict mode and `alert` to show how it prevents undeclared variables. |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## Summary |
| 67 | +- **Strict mode** helps catch errors and enforce better coding practices. |
| 68 | +- It prevents **accidental global variables** and **catches silent errors**. |
| 69 | +- It makes debugging easier and improves JavaScript performance. |
| 70 | + |
| 71 | +This chapter is presented by **Muhammad Raza** (SMRIT - Smart Modern Revolutionary IT Training). 🚀 |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## تعارف (Urdu) |
| 76 | + |
| 77 | +خوش آمدید **باب 3: جدید موڈ - "use strict"**۔ اس باب میں ہم سیکھیں گے کہ **strict mode** کیا ہے، یہ کیوں ضروری ہے، اور یہ کوڈنگ کو کیسے بہتر بناتا ہے۔ |
| 78 | + |
| 79 | +### "use strict" کیا ہے؟ |
| 80 | +جاوا اسکرپٹ کا **strict mode** کوڈ کو زیادہ محفوظ اور غلطیوں سے پاک بنانے میں مدد کرتا ہے۔ |
| 81 | + |
| 82 | +### Strict Mode کو فعال کرنے کا طریقہ |
| 83 | +اسے فعال کرنے کے لیے اسکرپٹ کے آغاز میں **"use strict"** لکھا جاتا ہے۔ |
| 84 | + |
| 85 | +#### مثال 1: اسکرپٹ میں Strict Mode |
| 86 | +```javascript |
| 87 | +"use strict"; |
| 88 | + |
| 89 | +let name = "Ali"; |
| 90 | +alert(name); |
| 91 | +``` |
| 92 | + |
| 93 | +#### مثال 2: Strict Mode کی روک تھام |
| 94 | +```javascript |
| 95 | +"use strict"; |
| 96 | + |
| 97 | +let age = 30; |
| 98 | +alert(age); |
| 99 | +``` |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Strict Mode کے فوائد |
| 104 | +1. **غلطی سے گلوبل ویری ایبل بننے سے بچاؤ** |
| 105 | + ```javascript |
| 106 | + "use strict"; |
| 107 | + let x = 10; |
| 108 | + alert(x); |
| 109 | + ``` |
| 110 | + |
| 111 | +2. **چھپی ہوئی غلطیوں کو پکڑتا ہے** |
| 112 | + ```javascript |
| 113 | + "use strict"; |
| 114 | + let y = "Hello"; |
| 115 | + alert(y); |
| 116 | + ``` |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## اسائنمنٹس |
| 121 | +1. **strict mode** کو جاوا اسکرپٹ میں فعال کریں اور `alert` کے ذریعے ٹیسٹ کریں۔ |
| 122 | +2. ایسا پروگرام لکھیں جہاں strict mode **غلطی سے گلوبل ویری ایبل بننے سے روکے** اور `alert` کا استعمال کریں۔ |
| 123 | +3. ایک ایسا اسکرپٹ لکھیں جو **strict mode اور non-strict mode کے فرق کو ظاہر کرے** اور `alert` استعمال کرے۔ |
| 124 | +4. ایک ایسا جاوا اسکرپٹ اسکرپٹ لکھیں جہاں `let` اور `alert` کا استعمال کرتے ہوئے strict mode کی غلطیوں کو دکھایا جائے۔ |
| 125 | +5. `use strict` کا استعمال کرتے ہوئے `alert` کے ذریعے ثابت کریں کہ یہ غیر اعلان شدہ ویری ایبلز کو کیسے روکتا ہے۔ |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +## خلاصہ |
| 130 | +- **Strict mode** غلطیوں کو پکڑنے اور بہتر کوڈنگ کے لیے ضروری ہے۔ |
| 131 | +- یہ **گلوبل ویری ایبلز، اور چھپی ہوئی غلطیوں** سے بچاتا ہے۔ |
| 132 | +- یہ جاوا اسکرپٹ کی کارکردگی اور سیکیورٹی کو بہتر بناتا ہے۔ |
| 133 | + |
| 134 | +یہ باب **محمد رضا** (SMRIT - Smart Modern Revolutionary IT Training) کی جانب سے پیش کیا گیا ہے۔ 🚀 |
| 135 | + |
0 commit comments