Operators and Control Flow Statement

Introduction to Operators

JavaScript, एक Versatile और Dynamic Programming Language के रूप में Data के Manipulation और विभिन्न Task का Execution करने की सुविधा के लिए Operator की एक Wide Array को Utilize करती है। JavaScript में Operator को विभिन्न Type में Categorized किया जा सकता है, जिनमें से प्रत्येक Language के Syntax में एक Specific Object को Fulfill करता है।

Type of Operators

Arithmetic Operators

Arithmetic Operators, Basic Mathematical Operation करते हैं। इसमें Addition (+), Subtraction (-), Multiplication (*), Division (/), और Modulus Operator (%) शामिल हैं, जो Division का Remainder Return करता है।

Example:


let a = 10;
let b = 3;

console.log(a + b);   // Output: 13
console.log(a – b);   // Output: 7
console.log(a * b);  // Output: 30
console.log(a / b);   // Output: 3.3333333333333335
console.log(a % b); // Output: 1

Comparison Operators

Comparison Operators का Use Value को Compare करने और Boolean Result Return करने के लिए किया जाता है। इनमें Equality (==), Strict Equality (===), Inequality (!=), Strict Inequality (!==), Greater Than (>), Less Than (<), Greater Than or Equal To (>=), और Less Than or Equal To (<=) से अधिक या इसके बराबर शामिल हैं।

Example:


let x = 5;
let y = “5”;

console.log(x == y);     // Output: true
console.log(x === y);  // Output: false
console.log(x != y);     // Output: false
console.log(x !== y);  // Output: true
console.log(x > y);     // Output: false
console.log(x < y);     // Output: false
console.log(x >= y);   // Output: true
console.log(x <= y);  // Output: true

Logical Operators

Logical Operators का Use Boolean Values को Combine या Manipulate करने के लिए किया जाता है। Logical AND (&&), Logical OR (||), और Logical NOT (!) Operator का use Complex Decision लेने के किया जाता हैं।

Example:


let p = true;
let q = false;

console.log(p && q);    // Output: false
console.log(p || q);     // Output: true
console.log(!p);            // Output: false

Assignment Operators

Assignment Operators का Use Variable को Value Assign करने के लिए किया जाता है। Simple Assignment (=) को +=, -=, *=, और /= जैसे Compound Assignment Operators द्वारा Augmented किया जाता है।

Example:


let num = 5;

num += 3;                     // Equivalent to num = num + 3
console.log(num);       // Output: 8

num *= 2;                    // Equivalent to num = num * 2
console.log(num);      // Output: 16

Unary Operators

Unary Operators एक ही Operand पर काम करते हैं। Unary Plus (+) और Unary Minus (-) किसी Number के Sign को बदलते हैं, जबकि Increment (++) और Decrement (–) Operator किसी Variable को 1 से बढ़ाते या घटाते हैं।

Example:


let counter = 10;

console.log(+counter);         // Output: 10
console.log(-counter);          // Output: -10

counter++;
console.log(counter);            // Output: 11

counter–;
console.log(counter);            // Output: 10

Control Flow Statements

Control Flow Statement, Web Development में Fundamental Structure हैं| जो उस Sequence को निर्धारित करते हैं, जिसमें किसी Program के भीतर Instruction को Execute किया जाता है। ये Statement, Developer को कुछ Condition के आधार पर Execution के Flow को Control करने की अनुमति देते हैं, जिससे Web Application अधिक Dynamic और Responsive बन जाते हैं।

Conditional Statement Developer को Defined Condition के आधार पर Code के कुछ Block को Execute करने की अनुमति देते हैं। सबसे Most Common Conditional Statement हैं –

if Statement

if Statement, Conditional Statement का सबसे Basic Type है। यह एक Condition को Evaluate करता है, और यदि Condition True है, तो Code के एक Block को Execute करता है। अथवा यह Code के Block को Skip कर देता है, और Next Statement पर चला जाता है।

Syntax:


if (condition) {
// Code block to be executed if Condition is true
}

Example:


var age = 25;
if (age >= 18) {
console.log(“You are eligible to vote”);
}

if-else Statement

if-else Statement का use JavaScript में Decision लेने के लिए किया जाता है। if Condition True है, तो यह आपको Code के एक Block को Execute करने की Permission देता है|और यदि Condition False है, तो दूसरे Block को Execute करने की Permission देता है।

Syntax:


if (condition) {
// Code block to be executed if Condition is true
} else {
// Code block to be executed if Condition is false
}

Example:


let x = 10;

if (x > 5) {
console.log(“x is greater than 5”);
} else {
console.log(“x is not greater than 5”);
}

इस Example में, यदि x का Value 5 से अधिक है, तो Code का पहला Block Execute किया जाएगा; अन्यथा, दूसरा Block Execute किया जाएगा।

Switch Statement

Switch Statement का उपयोग विभिन्न Condition के आधार पर विभिन्न Action करने के लिए किया जाता है। यह if-else statement की Long Chain का एक अधिक Efficient Alternative है।

Syntax:


switch (expression) {
case value1:
// Code block to be executed if expression matches value1
break;
case value2:
// Code block to be executed if expression matches value2
break;
default:
// Code block to be executed if expression doesn’t match any case
}

Looping Statements

Looping Statement का use Code के एक Block को बार-बार Execute करने के लिए किया जाता है, जब तक कि एक Specified Condition True होती है। Common Looping Statement में शामिल हैं-

for Loop

for Loop का उपयोग किसी Array जैसे Element के Sequence पर पुनरावृत्ति करने के लिए किया जाता है। इसमें एक Initialization Statement, एक Condition और एक Increment/Decrement Statement शामिल होता है।

Syntax:


for (initialization; condition; increment/decrement) {
// Code block to be executed
}

Example:


for (let i = 0; i < 5; i++) {
console.log(i);
}

यह Loop Value को 0 से 4 तक Print करेगा। Initialization Let (i=0) Starting Point Set करता है, Condition (i<5) निर्धारित करती है, कि Loop कब रुकना चाहिए| और (i++) प्रत्येक पुनरावृत्ति के बाद (i) का Value बढ़ाता है।

while Loop

एक while Loop Code के एक Block को Repeat करता है, जब Specified Condition True होती है। इसमें केवल एक Conditon होता है| और Loop तब तक जारी रहता है, जब तक वह Condition True है।

Syntax:


while (condition) {
// Code block to be executed
}

Example:


let i = 0;

while (i < 5) {
console.log(i);
i++;
}

यह Loop Functionally रूप से उपरोक्त Loop Example के बराबर है। यह 0 से 4 तक Value Print करता है ,जबकि Condition (i <5) True है।

do-while Loop

do-while Loop, while Loop के समान है, लेकिन Code के Block Execute होने के बाद Condtion की Check की जाती है। यह सुनिश्चित करता है, कि Block कम से कम एक बार Execute हो।

Syntax:


do {
// Code block to be executed
} while (condition);

Example:


let i = 0;

do {
console.log(i);
i++;
} while (i < 5);

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *