Q. What is JavaScript? Its Advantages and Disadvantages.
In English:
JavaScript is a scripting language used to make web pages interactive and dynamic. It helps in controlling the behavior of web pages such as form validation, animations, pop-ups, and event handling. It runs directly inside the web browser.
Advantages:
- Makes web pages interactive and attractive.
- Runs directly in the browser, no need for extra software.
- Increases the speed of web applications.
- Easy to learn and use with HTML.
- Works on both client and server sides (using Node.js).
Disadvantages:
- Code is visible to users, so it is less secure.
- Can be disabled in browsers, which may stop functions.
- Large scripts can slow down the page.
- Browser compatibility issues may occur.
हिन्दी में:
JavaScript एक स्क्रिप्टिंग भाषा है जो वेब पेज को इंटरैक्टिव और डायनेमिक बनाती है। यह वेब पेज के व्यवहार को नियंत्रित करती है, जैसे फॉर्म जाँचना, एनिमेशन, पॉप-अप और इवेंट हैंडलिंग। यह सीधे वेब ब्राउज़र में चलती है।
फायदे:
- वेब पेज को आकर्षक और इंटरैक्टिव बनाती है।
- किसी अतिरिक्त सॉफ्टवेयर की आवश्यकता नहीं होती।
- वेब एप्लिकेशन की गति बढ़ाती है।
- HTML के साथ सीखना और उपयोग करना आसान है।
- क्लाइंट और सर्वर दोनों पर काम कर सकती है (Node.js द्वारा)।
नुकसान:
- कोड यूज़र को दिखाई देता है, इसलिए सुरक्षा कम होती है।
- यदि ब्राउज़र में JavaScript बंद हो, तो कार्य रुक सकते हैं।
- बड़ा स्क्रिप्ट पेज की गति कम कर सकता है।
- अलग-अलग ब्राउज़रों में कोड सही से न चलने की संभावना रहती है।
Q. Difference between Client Side Scripting and Server Side Scripting
In English:
Client Side Scripting and Server Side Scripting are two types of web scripting used to create dynamic web pages. The main difference is where the code runs — on the user’s browser or on the server.
| Client Side Scripting | Server Side Scripting |
|---|---|
| Code runs on the user’s web browser. | Code runs on the web server. |
| Used for user interaction and page design. | Used for database connection and data processing. |
| Executes quickly because no server request is needed. | Slower because it sends requests to the server. |
| Example languages: JavaScript, VBScript. | Example languages: PHP, ASP, Python. |
| Cannot access databases directly. | Can access and manage databases. |
| Output is visible to users. | Output is sent to the user after processing. |
| Less secure because code is visible. | More secure because code runs on the server. |
| Depends on the browser for execution. | Depends on the server for execution. |
हिन्दी में:
Client Side Scripting और Server Side Scripting दो प्रकार की वेब स्क्रिप्टिंग हैं जिनका उपयोग डायनेमिक वेब पेज बनाने के लिए किया जाता है। इन दोनों में मुख्य अंतर यह है कि कोड कहाँ चलता है — यूज़र के ब्राउज़र पर या सर्वर पर।
| Client Side Scripting | Server Side Scripting |
|---|---|
| कोड यूज़र के वेब ब्राउज़र में चलता है। | कोड वेब सर्वर पर चलता है। |
| यूज़र इंटरैक्शन और पेज डिज़ाइन के लिए उपयोग होता है। | डेटाबेस कनेक्शन और डेटा प्रोसेसिंग के लिए उपयोग होता है। |
| तेज़ चलता है क्योंकि सर्वर से अनुरोध नहीं भेजता। | थोड़ा धीमा होता है क्योंकि सर्वर से अनुरोध भेजना पड़ता है। |
| उदाहरण भाषाएँ: JavaScript, VBScript. | उदाहरण भाषाएँ: PHP, ASP, Python. |
| डेटाबेस को सीधे एक्सेस नहीं कर सकता। | डेटाबेस को एक्सेस और प्रबंधित कर सकता है। |
| आउटपुट यूज़र को दिखाई देता है। | आउटपुट प्रोसेस होने के बाद यूज़र को भेजा जाता है। |
| सुरक्षा कम होती है क्योंकि कोड दिखाई देता है। | अधिक सुरक्षित होता है क्योंकि कोड सर्वर पर चलता है। |
| निष्पादन के लिए ब्राउज़र पर निर्भर। | निष्पादन के लिए सर्वर पर निर्भर। |
Q. What is Variable? Write a JavaScript program to illustrate the variable.
In English:
A variable in JavaScript is a container that stores data or information which can be used and changed later in the program.
It is like a box that holds a value (such as number, text, etc.).
👉 In simple words:
A variable is used to store data temporarily in memory.
Syntax:
var variableName = value;
let variableName = value;
const variableName = value;
Example Program:
// JavaScript program to illustrate variables
var name = "Gopal"; // String variable
let age = 20; // Number variable
const country = "India"; // Constant variable
document.write("Name: " + name + "<br>");
document.write("Age: " + age + "<br>");
document.write("Country: " + country);
Output:
Name: Gopal
Age: 20
Country: India
हिन्दी में:
Variable JavaScript में एक कंटेनर (Container) होता है जो डेटा या जानकारी को स्टोर (संग्रह) करता है।
इसे ऐसे समझो जैसे एक डिब्बा (box) जिसमें कोई भी मान (value) रखी जाती है।
👉 सरल शब्दों में:
Variable का उपयोग अस्थायी रूप से डेटा को मेमोरी में रखने के लिए किया जाता है।
Syntax (वाक्य रचना):
var variableName = value;
let variableName = value;
const variableName = value;
उदाहरण प्रोग्राम:
// Variable को दर्शाने वाला JavaScript प्रोग्राम
var name = "Gopal"; // स्ट्रिंग प्रकार का वेरिएबल
let age = 20; // नंबर प्रकार का वेरिएबल
const country = "India"; // स्थिर वेरिएबल (Constant)
document.write("Name: " + name + "<br>");
document.write("Age: " + age + "<br>");
document.write("Country: " + country);
आउटपुट:
Name: Gopal
Age: 20
Country: India
Q. What is Data Type in JavaScript?
In English:
A data type in JavaScript defines the kind of value a variable can hold.
It tells the computer what type of data is being stored — for example, number, text, true/false, etc.
👉 In simple words:
Data types tell JavaScript what kind of value is stored in a variable.
Types of Data Types in JavaScript:
| Type | Description | Example |
|---|---|---|
| 1. String | Text values written in quotes | "Hello", 'Gopal' |
| 2. Number | Numeric values (with or without decimal) | 10, 5.5 |
| 3. Boolean | True or False values | true, false |
| 4. Undefined | When a variable is declared but no value is assigned | var x; |
| 5. Null | Represents an empty or no value | var x = null; |
| 6. Object | Collection of data in key-value pairs | {name: "Gopal", age: 20} |
| 7. Array | Used to store multiple values in one variable | [1, 2, 3, 4] |
Example Program:
var name = "Gopal"; // String
var age = 20; // Number
var isStudent = true; // Boolean
var address; // Undefined
var marks = null; // Null
document.write(typeof name + "<br>");
document.write(typeof age + "<br>");
document.write(typeof isStudent + "<br>");
document.write(typeof address + "<br>");
document.write(typeof marks);
Output:
string
number
boolean
undefined
object
हिन्दी में:
Data Type JavaScript में यह बताता है कि कोई variable किस प्रकार का मान (value) रखता है।
यह कंप्यूटर को बताता है कि डेटा संख्या है, टेक्स्ट है या कोई और प्रकार का है।
👉 सरल शब्दों में:
Data Type यह बताता है कि वेरिएबल में किस तरह का डेटा रखा गया है।
JavaScript के मुख्य Data Types:
| प्रकार (Type) | विवरण (Description) | उदाहरण (Example) |
|---|---|---|
| 1. String | टेक्स्ट के लिए उपयोग किया जाता है | "Hello", 'Gopal' |
| 2. Number | संख्यात्मक मान (नंबर) | 10, 5.5 |
| 3. Boolean | True या False मान | true, false |
| 4. Undefined | जब वेरिएबल घोषित हो लेकिन मान न दिया जाए | var x; |
| 5. Null | खाली या कोई मान नहीं | var x = null; |
| 6. Object | डेटा को key-value रूप में संग्रहित करता है | {name: "Gopal", age: 20} |
| 7. Array | एक वेरिएबल में कई मान रखता है | [1, 2, 3, 4] |
Example Program (उदाहरण प्रोग्राम):
var name = "Gopal"; // स्ट्रिंग
var age = 20; // नंबर
var isStudent = true; // बूलियन
var address; // अनडिफाइंड
var marks = null; // नल
document.write(typeof name + "<br>");
document.write(typeof age + "<br>");
document.write(typeof isStudent + "<br>");
document.write(typeof address + "<br>");
document.write(typeof marks);
आउटपुट:
string
number
boolean
undefined
object
Q. What is Conditional Operator in JavaScript?
In English:
A conditional operator in JavaScript is also called the ternary operator because it uses three operands.
It is used to check a condition and return one value if the condition is true and another value if it is false.
👉 It is a short form of if-else statement.
Syntax:
condition ? value_if_true : value_if_false;
Example:
var age = 18;
var result = (age >= 18) ? "Eligible to Vote" : "Not Eligible";
document.write(result);
Output:
Eligible to Vote
Explanation:
- If the condition
(age >= 18)is true, it prints “Eligible to Vote”. - Otherwise, it prints “Not Eligible”.
In Hindi (हिन्दी में):
Conditional Operator को JavaScript में Ternary Operator भी कहा जाता है क्योंकि इसमें तीन भाग होते हैं।
यह किसी शर्त (condition) की जाँच करता है और सही होने पर एक मान तथा गलत होने पर दूसरा मान लौटाता है।
👉 इसे if-else statement का छोटा रूप कहा जा सकता है।
Syntax (संरचना):
condition ? value_if_true : value_if_false;
Example (उदाहरण):
var age = 18;
var result = (age >= 18) ? "Vote करने योग्य" : "Vote करने योग्य नहीं";
document.write(result);
Output:
Vote करने योग्य
Explanation (व्याख्या):
- अगर
(age >= 18)सही है, तो यह “Vote करने योग्य” दिखाएगा। - अगर यह गलत है, तो यह “Vote करने योग्य नहीं” दिखाएगा।
👉 Memory Trick:
Ternary = 3 parts → Condition ? True : False
(Think: “If true do this, else do that.”)
Ok my friend 👍
Now I will write in exact direct exam format as you want.
What is Conditional Operator?
Answer
The conditional operator is a decision-making operator in JavaScript. It is also called the ternary operator. It is used to check a condition in one line. This operator works like an if-else statement. It uses two symbols. These symbols are question mark (?) and colon (:). If the condition is true, the first expression runs. If the condition is false, the second expression runs. This operator makes the code short and simple. It is easy to use and understand.
Syntax
condition ? expression1 : expression2;
Example
let marks = 50;
let result = (marks >= 33) ? “Pass” : “Fail”;
Output Explanation
If marks are 33 or more, output will be Pass.
If marks are less than 33, output will be Fail.
Conditional Operator क्या है?
उत्तर
Conditional operator जावास्क्रिप्ट में एक decision-making operator होता है। इसे ternary operator भी कहा जाता है। इसका उपयोग एक ही लाइन में condition check करने के लिए किया जाता है। यह operator if-else statement की तरह काम करता है। इसमें दो symbols का प्रयोग होता है। ये symbols question mark (?) और colon (:) होते हैं। अगर condition true होती है तो पहली expression run होती है। अगर condition false होती है तो दूसरी expression run होती है। यह operator कोड को छोटा और आसान बनाता है। इसे उपयोग करना और समझना आसान होता है।
Syntax
condition ? expression1 : expression2;
Example
let marks = 50;
let result = (marks >= 33) ? “Pass” : “Fail”;
Output Explanation
अगर marks 33 या उससे ज्यादा होंगे तो output Pass होगा।
अगर marks 33 से कम होंगे तो output Fail होगा।
Ok my friend 👍
Here is the difference between If-Else and Switch Statement in table format, ready to copy-paste.
What is the difference between If-Else and Switch Statement?
Answer
| Feature | If-Else Statement | Switch Statement |
|---|---|---|
| Definition | Executes code based on a condition | Executes code based on the value of a variable |
| Condition Type | Boolean expressions (true/false) | Exact values (number, string) |
| Multiple Conditions | Can handle multiple conditions using else-if | Can handle multiple cases with discrete values |
| Complex Conditions | Can handle ranges and logical operators | Cannot handle ranges or complex conditions |
| Readability | Can become long with many conditions | Cleaner and easier to read with many cases |
| Use Case | Best for range checks and complex logic | Best for exact value checks |
If-Else और Switch Statement में क्या अंतर है?
उत्तर
| विशेषता | If-Else Statement | Switch Statement |
|---|---|---|
| परिभाषा | Condition के आधार पर कोड execute करता है | Variable के value के आधार पर कोड execute करता है |
| Condition का प्रकार | Boolean expressions (true/false) | Exact values (number, string) |
| Multiple Conditions | Else-if का उपयोग करके कई conditions handle कर सकता है | Multiple cases के लिए discrete values handle कर सकता है |
| Complex Conditions | Range और logical operators handle कर सकता है | Range या complex conditions handle नहीं कर सकता |
| पढ़ने में सरलता | बहुत सारी conditions में लंबा हो सकता है | बहुत सारे cases में clean और easy to read है |
| Use Case | Range checks और complex logic के लिए best | Exact value checks के लिए best |
Ok my friend 👍
Here is the difference between If-Else and Switch Statement in table format, ready to copy-paste.
What is the difference between If-Else and Switch Statement?
Answer
| Feature | If-Else Statement | Switch Statement |
|---|---|---|
| Definition | Executes code based on a condition | Executes code based on the value of a variable |
| Condition Type | Boolean expressions (true/false) | Exact values (number, string) |
| Multiple Conditions | Can handle multiple conditions using else-if | Can handle multiple cases with discrete values |
| Complex Conditions | Can handle ranges and logical operators | Cannot handle ranges or complex conditions |
| Readability | Can become long with many conditions | Cleaner and easier to read with many cases |
| Use Case | Best for range checks and complex logic | Best for exact value checks |
If-Else और Switch Statement में क्या अंतर है?
उत्तर
| विशेषता | If-Else Statement | Switch Statement |
|---|---|---|
| परिभाषा | Condition के आधार पर कोड execute करता है | Variable के value के आधार पर कोड execute करता है |
| Condition का प्रकार | Boolean expressions (true/false) | Exact values (number, string) |
| Multiple Conditions | Else-if का उपयोग करके कई conditions handle कर सकता है | Multiple cases के लिए discrete values handle कर सकता है |
| Complex Conditions | Range और logical operators handle कर सकता है | Range या complex conditions handle नहीं कर सकता |
| पढ़ने में सरलता | बहुत सारी conditions में लंबा हो सकता है | बहुत सारे cases में clean और easy to read है |
| Use Case | Range checks और complex logic के लिए best | Exact value checks के लिए best |
What is For Loop in JavaScript?
Answer
A for loop in JavaScript is used to repeat a block of code a specific number of times. It is useful when the number of iterations is known. The for loop has three parts: initialization, condition, and increment/decrement. First, the loop initializes a variable. Then it checks the condition before each iteration. If the condition is true, the loop runs the code block. After each iteration, the loop updates the variable using increment or decrement.
Syntax
for(initialization; condition; increment/decrement) {
// code to run
}
Example
for(let i = 1; i <= 5; i++) {
console.log(i);
}
Output Explanation
The code prints numbers from 1 to 5. First, i = 1, then i increases by 1 each time until it reaches 5.
JavaScript में For Loop क्या है?
उत्तर
JavaScript में for loop का उपयोग कोड के एक block को एक निश्चित संख्या में बार repeat करने के लिए किया जाता है। यह तब उपयोगी होता है जब iteration की संख्या पहले से पता हो। For loop के तीन हिस्से होते हैं: initialization, condition, और increment/decrement। पहले loop एक variable initialize करता है। फिर हर iteration से पहले condition check करता है। अगर condition true होती है तो loop code block चलाता है। हर iteration के बाद variable को increment या decrement करके update किया जाता है।
Syntax
for(initialization; condition; increment/decrement) {
// चलने वाला कोड
}
Example
for(let i = 1; i <= 5; i++) {
console.log(i);
}
Output Explanation
Code 1 से 5 तक numbers print करता है। पहले i = 1, फिर हर बार i 1 बढ़ता है जब तक कि यह 5 तक न पहुँच जाए।
What is the difference between While and Do..While loop in JavaScript?
Answer
| Feature | While Loop | Do..While Loop |
|---|---|---|
| Definition | Repeats a block of code while the condition is true | Repeats a block of code at least once and then while the condition is true |
| Condition Check | Condition is checked before executing the code | Condition is checked after executing the code |
| Execution | May not execute at all if condition is false | Executes at least once even if condition is false |
| Use Case | When you want to check condition before running the loop | When you want the loop to run at least once regardless of condition |
| Syntax | while(condition) { //code } | do { //code } while(condition); |
JavaScript में While और Do..While loop में क्या अंतर है?
उत्तर
| विशेषता | While Loop | Do..While Loop |
|---|---|---|
| परिभाषा | कोड का block तब तक repeat करता है जब तक condition true हो | कोड का block कम से कम एक बार चलता है और फिर condition true होने तक repeat होता है |
| Condition Check | Condition code execute करने से पहले check होती है | Condition code execute करने के बाद check होती है |
| Execution | Condition false होने पर loop कभी execute नहीं हो सकता | कम से कम एक बार loop execute होता है, चाहे condition false हो |
| Use Case | जब loop चलाने से पहले condition check करना हो | जब loop कम से कम एक बार चले, चाहे condition false हो |
| Syntax | while(condition) { //कोड } | do { //कोड } while(condition); |
What is JavaScript Validation?
Answer
JavaScript Validation is the process of checking the data entered by a user in a form before sending it to the server. It helps to ensure that the user fills the form correctly. Validation can check if a field is empty, if the data is in the correct format, or if a value is within a specific range. Using JavaScript for validation improves user experience because errors can be shown immediately without refreshing the page.
Example
function validateForm() {
let name = document.forms["myForm"]["username"].value;
if (name == "") {
alert("Name must be filled out");
return false;
}
}
Output Explanation
If the user leaves the name field empty, an alert will appear saying “Name must be filled out”. The form will not submit until the field is filled.
JavaScript Validation क्या है?
उत्तर
JavaScript Validation एक process है जो form में user द्वारा डाले गए data को server पर भेजने से पहले check करती है। इसका उद्देश्य यह सुनिश्चित करना है कि user form को सही तरीके से भरे। Validation यह check कर सकती है कि कोई field खाली तो नहीं है, data सही format में है या value किसी specific range में है। JavaScript से validation करने से user experience बेहतर होता है क्योंकि errors तुरंत दिखाई देते हैं बिना page refresh किए।
Example
function validateForm() {
let name = document.forms["myForm"]["username"].value;
if (name == "") {
alert("Name must be filled out");
return false;
}
}
Output Explanation
अगर user name field खाली छोड़ देता है, तो alert आएगा “Name must be filled out”। Form submit नहीं होगा जब तक field भर न दिया जाए।
What is Cookies in JavaScript?
Answer
Cookies are small pieces of data stored by a website on the user’s browser. They are used to remember information about the user, like login details, preferences, or shopping cart items. Cookies help websites provide a better user experience. JavaScript can create, read, and delete cookies using document.cookie. Cookies have a name, value, expiration date, and path.
Example
// Create a cookie
document.cookie = "username=Gopal; expires=Fri, 31 Jan 2026 12:00:00 UTC; path=/";
// Read cookies
console.log(document.cookie);
// Delete a cookie
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";
Output Explanation
- The first line creates a cookie named “username” with value “Gopal”.
- The second line reads all cookies.
- The third line deletes the cookie by setting an old expiration date.
JavaScript में Cookies क्या हैं?
उत्तर
Cookies छोटे डेटा के टुकड़े होते हैं जो वेबसाइट user के browser में store करती है। इनका उपयोग user की जानकारी याद रखने के लिए किया जाता है, जैसे login details, preferences, या shopping cart items। Cookies से वेबसाइट बेहतर user experience देती है। JavaScript से cookies create, read और delete की जा सकती हैं। Cookies में नाम, value, expiration date और path होता है।
Example
// Cookie बनाना
document.cookie = "username=Gopal; expires=Fri, 31 Jan 2026 12:00:00 UTC; path=/";
// Cookies पढ़ना
console.log(document.cookie);
// Cookie delete करना
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";
Output Explanation
- पहली line एक cookie बनाती है जिसका नाम “username” और value “Gopal” है।
- दूसरी line सभी cookies पढ़ती है।
- तीसरी line cookie delete करती है पुरानी expiration date देकर।
What is Operator? Explain different operators used in JavaScript.
Answer
An operator in JavaScript is a symbol that is used to perform operations on variables and values. Operators are used to do calculations, comparisons, and logical operations in a program. JavaScript provides many types of operators to perform different tasks.
Different Operators Used in JavaScript
- Arithmetic Operators
These operators are used to perform mathematical calculations.
Examples: +, -, *, /, %, ++, — - Assignment Operators
These operators are used to assign values to variables.
Examples: =, +=, -=, *=, /= - Comparison Operators
These operators are used to compare two values.
Examples: ==, ===, !=, >, <, >=, <= - Logical Operators
These operators are used to perform logical operations.
Examples: &&, ||, ! - Conditional Operator
This operator is used to check a condition in one line.
Example: condition ? value1 : value2 - Bitwise Operators
These operators work on binary values.
Examples: &, |, ^, ~ - String Operator
The + operator is used to join two strings.
प्रश्न (Hindi)
Operator क्या है? JavaScript में प्रयोग होने वाले विभिन्न operators को समझाइए।
उत्तर
JavaScript में operator एक symbol होता है जिसका उपयोग variables और values पर operations करने के लिए किया जाता है। Operators का उपयोग calculations, comparisons और logical operations करने के लिए किया जाता है। JavaScript में अलग-अलग कामों के लिए कई प्रकार के operators होते हैं।
JavaScript में प्रयोग होने वाले अलग-अलग Operators
- Arithmetic Operators
इनका उपयोग mathematical calculations करने के लिए किया जाता है।
उदाहरण: +, -, *, /, %, ++, — - Assignment Operators
इनका उपयोग variables को value देने के लिए किया जाता है।
उदाहरण: =, +=, -=, *=, /= - Comparison Operators
इनका उपयोग दो values को compare करने के लिए किया जाता है।
उदाहरण: ==, ===, !=, >, <, >=, <= - Logical Operators
इनका उपयोग logical operations करने के लिए किया जाता है।
उदाहरण: &&, ||, ! - Conditional Operator
इसका उपयोग एक line में condition check करने के लिए किया जाता है।
उदाहरण: condition ? value1 : value2 - Bitwise Operators
ये operators binary values पर काम करते हैं।
उदाहरण: &, |, ^, ~ - String Operator
- operator का उपयोग दो strings को जोड़ने के लिए किया जाता है।
Why JavaScript is known as Client Side Language?
Answer
JavaScript is known as a client side language because it runs on the user’s computer inside the web browser. It does not need a server to execute the code. JavaScript code is downloaded with the web page and executed directly by the browser. All processing and actions are performed on the client machine. It is mainly used to make web pages interactive and dynamic. Tasks like form validation, animations, and user interactions are handled on the client side using JavaScript. Because of these reasons, JavaScript is called a client side scripting language.
Main Reasons
- JavaScript runs in the browser.
- It does not require server support.
- It executes on the user computer.
- It makes web pages interactive.
- It reduces server load.
प्रश्न (Hindi)
JavaScript को Client Side Language क्यों कहा जाता है?
उत्तर
JavaScript को client side language इसलिए कहा जाता है क्योंकि यह user के computer पर web browser के अंदर run होती है। इसे code execute करने के लिए server की जरूरत नहीं होती है। JavaScript का code web page के साथ download होता है और सीधे browser द्वारा execute किया जाता है। सभी processing और actions client machine पर ही होते हैं। JavaScript का मुख्य उपयोग web pages को interactive और dynamic बनाने के लिए किया जाता है। Form validation, animations और user interactions जैसे काम client side पर JavaScript द्वारा किए जाते हैं। इन सभी कारणों से JavaScript को client side scripting language कहा जाता है।
मुख्य कारण
- JavaScript browser में run होती है।
- इसे server की जरूरत नहीं होती।
- यह user के computer पर execute होती है।
- यह web pages को interactive बनाती है।
- यह server का load कम करती है।
What is JavaScript Events?
Answer
JavaScript events are actions or occurrences that happen in the browser. These events are generated when a user interacts with a web page. Events allow JavaScript to respond to user actions. Examples of events are clicking a button, pressing a key, loading a page, or moving the mouse. JavaScript uses event handlers to perform actions when an event occurs. Events make web pages interactive and dynamic.
Common JavaScript Events
- onclick – when user clicks on an element
- onload – when a web page loads
- onchange – when value of an input changes
- onmouseover – when mouse moves over an element
- onkeydown – when a key is pressed
- onsubmit – when a form is submitted
Example
<button onclick="alert('Hello User')">Click Me</button>
Output Explanation
When the user clicks the button, an alert box will appear with the message “Hello User”.
JavaScript Events क्या हैं?
उत्तर
JavaScript events वे actions या घटनाएँ होती हैं जो browser में होती हैं। ये events तब generate होते हैं जब user web page के साथ interact करता है। Events की मदद से JavaScript user के actions का response दे सकती है। Button पर click करना, key press करना, page load होना या mouse move करना events के उदाहरण हैं। JavaScript event handlers का उपयोग करती है ताकि event होने पर कोई action perform किया जा सके। Events web pages को interactive और dynamic बनाते हैं।
सामान्य JavaScript Events
- onclick – जब user किसी element पर click करता है
- onload – जब web page load होता है
- onchange – जब input की value change होती है
- onmouseover – जब mouse element के ऊपर जाता है
- onkeydown – जब कोई key press की जाती है
- onsubmit – जब form submit किया जाता है
Example
<button onclick="alert('Hello User')">Click Me</button>
Output Explanation
जब user button पर click करेगा तो “Hello User” message वाला alert box दिखाई देगा।
Why JavaScript is called Interpreted Language?
Answer
JavaScript is called an interpreted language because its code is executed line by line by the browser without compilation. JavaScript does not need to be converted into machine code before running. The JavaScript interpreter in the browser reads and executes the code directly. This makes JavaScript easy to test and debug. Changes in JavaScript code can be seen immediately without recompiling. Because of this line-by-line execution process, JavaScript is known as an interpreted language.
Main Reasons
- JavaScript code is executed line by line.
- It does not require compilation.
- It is executed directly by the browser.
- Errors are shown during execution.
- Code changes can be tested quickly.
प्रश्न (Hindi)
JavaScript को Interpreted Language क्यों कहा जाता है?
उत्तर
JavaScript को interpreted language इसलिए कहा जाता है क्योंकि इसका code compilation के बिना browser द्वारा line by line execute किया जाता है। JavaScript को run करने से पहले machine code में बदलने की जरूरत नहीं होती। Browser में मौजूद JavaScript interpreter code को सीधे पढ़कर execute करता है। इससे JavaScript को test और debug करना आसान हो जाता है। JavaScript code में किए गए changes तुरंत दिखाई देते हैं और recompiling की जरूरत नहीं पड़ती। इस line-by-line execution process के कारण JavaScript को interpreted language कहा जाता है।
मुख्य कारण
- JavaScript code line by line execute होता है।
- इसे compilation की जरूरत नहीं होती।
- यह सीधे browser द्वारा run किया जाता है।
- Errors execution के समय दिखाई देती हैं।
- Code changes को जल्दी test किया जा सकता है।
Question (English)
What is the difference between Compiled and Interpreted Language?
Answer
| Feature | Compiled Language | Interpreted Language |
|---|---|---|
| Execution Process | Code is converted into machine code before execution | Code is executed line by line |
| Translator Used | Uses a compiler | Uses an interpreter |
| Speed | Faster execution | Slower execution |
| Error Detection | Errors are shown after full compilation | Errors are shown during execution |
| Conversion | Whole program is translated at once | Program is translated line by line |
| Example Languages | C, C++, Java | JavaScript, Python, PHP |
In compiled languages, the entire source code is converted into machine code before running. In interpreted languages, the code is executed directly without prior compilation.
प्रश्न (Hindi)
Compiled और Interpreted Language में क्या अंतर है?
उत्तर
| विशेषता | Compiled Language | Interpreted Language |
|---|---|---|
| Execution Process | Code execution से पहले machine code में बदलता है | Code line by line execute होता है |
| Translator Used | Compiler का उपयोग होता है | Interpreter का उपयोग होता है |
| Speed | Execution तेज होती है | Execution धीमी होती है |
| Error Detection | Errors compilation के बाद दिखती हैं | Errors execution के समय दिखती हैं |
| Conversion | पूरा program एक बार में translate होता है | Program एक-एक line करके translate होता है |
| Example Languages | C, C++, Java | JavaScript, Python, PHP |
Compiled languages में पूरा source code पहले machine code में बदलता है और फिर run होता है। Interpreted languages में code सीधे line by line execute होता है बिना compilation के।
Ok my friend 👍
Here is your answer in direct copy-paste exam format as you asked.
Explain :
Comments, Conditional Operator, Logical Operator, String Operator, Bitwise Operator, Break and Continue Statement.
Answer
1. Comments
Comments are lines in a program that are not executed by JavaScript. They are used to explain the code and make it easy to understand. There are two types of comments in JavaScript.
Types of Comments
- Single line comment: //
- Multi line comment: /* */
Example
// This is a single line comment
/* This is a multi line comment */
2. Conditional Operator
The conditional operator is also called the ternary operator. It is used to check a condition in one line. It works like an if-else statement.
Syntax
condition ? expression1 : expression2;
Example
let result = (age >= 18) ? "Adult" : "Minor";
3. Logical Operator
Logical operators are used to combine two or more conditions. They return true or false.
Types of Logical Operators
- && (AND)
- || (OR)
- ! (NOT)
Example
if (age > 18 && marks > 40)
4. String Operator
The string operator is used to join two or more strings. In JavaScript, the + operator is used as a string operator.
Example
let name = "Ram" + " Kumar";
5. Bitwise Operator
Bitwise operators work on binary numbers. They perform operations at bit level.
Examples of Bitwise Operators
- & (AND)
- | (OR)
- ^ (XOR)
- ~ (NOT)
6. Break Statement
The break statement is used to stop a loop or switch statement immediately.
Example
for(let i=1; i<=5; i++) {
if(i==3) {
break;
}
}
7. Continue Statement
The continue statement is used to skip the current iteration of a loop and move to the next iteration.
Example
for(let i=1; i<=5; i++) {
if(i==3) {
continue;
}
}
प्रश्न (Hindi)
Comments, Conditional Operator, Logical Operator, String Operator, Bitwise Operator, Break और Continue Statement को समझाइए।
उत्तर
1. Comments
Comments program की वे lines होती हैं जो JavaScript द्वारा execute नहीं होतीं। इनका उपयोग code को समझाने और आसान बनाने के लिए किया जाता है। JavaScript में दो प्रकार के comments होते हैं।
Comments के प्रकार
- Single line comment: //
- Multi line comment: /* */
Example
// यह single line comment है
/* यह multi line comment है */
2. Conditional Operator
Conditional operator को ternary operator भी कहा जाता है। इसका उपयोग एक ही line में condition check करने के लिए किया जाता है। यह if-else statement की तरह काम करता है।
Syntax
condition ? expression1 : expression2;
Example
let result = (age >= 18) ? "Adult" : "Minor";
3. Logical Operator
Logical operators का उपयोग दो या दो से अधिक conditions को जोड़ने के लिए किया जाता है। ये true या false return करते हैं।
Logical Operators के प्रकार
- && (AND)
- || (OR)
- ! (NOT)
Example
if (age > 18 && marks > 40)
4. String Operator
String operator का उपयोग दो या अधिक strings को जोड़ने के लिए किया जाता है। JavaScript में + operator string operator के रूप में काम करता है।
Example
let name = "Ram" + " Kumar";
5. Bitwise Operator
Bitwise operators binary numbers पर काम करते हैं। ये bit level पर operations perform करते हैं।
Bitwise Operators के उदाहरण
- & (AND)
- | (OR)
- ^ (XOR)
- ~ (NOT)
6. Break Statement
Break statement का उपयोग loop या switch statement को तुरंत रोकने के लिए किया जाता है।
Example
for(let i=1; i<=5; i++) {
if(i==3) {
break;
}
}
7. Continue Statement
Continue statement का उपयोग loop की current iteration को skip करके अगली iteration पर जाने के लिए किया जाता है।
Example
for(let i=1; i<=5; i++) {
if(i==3) {
continue;
}
}
Write the following programs in JavaScript with explanation and working.
Write the following programs in JavaScript :
- To check a person is eligible for vote or not
- Adult or not
- Print first 20 numbers using for loop
- Print sum of first 10 numbers
- Print sum of first 10 even numbers using while loop
- Use of if else if statement
- Switch statement
Answer
1. Program to check a person is eligible for vote or not
<html>
<body>
<script language="javascript">
var age = 20;
if(age >= 18)
{
document.write("Eligible for Vote");
}
else
{
document.write("Not Eligible for Vote");
}
</script>
</body>
</html>
Explanation and Working
This program checks the age of a person. If age is 18 or more, it prints Eligible for Vote. If age is less than 18, it prints Not Eligible for Vote.
2. Program to check Adult or Not
<html>
<body>
<script language="javascript">
var age = 16;
if(age >= 18)
{
document.write("Adult");
}
else
{
document.write("Not Adult");
}
</script>
</body>
</html>
Explanation and Working
This program checks whether a person is adult or not. If age is 18 or more, output will be Adult. Otherwise output will be Not Adult.
3. Program to print first 20 numbers using for loop
<html>
<body>
<script language="javascript">
for(var i=1; i<=20; i++)
{
document.write(i + "<br>");
}
</script>
</body>
</html>
Explanation and Working
This program uses for loop. It starts from 1 and prints numbers up to 20 one by one.
4. Program to print sum of first 10 numbers
<html>
<body>
<script language="javascript">
var sum = 0;
for(var i=1; i<=10; i++)
{
sum = sum + i;
}
document.write("Sum of first 10 numbers = " + sum);
</script>
</body>
</html>
Explanation and Working
This program adds numbers from 1 to 10. The result is stored in variable sum and printed at the end.
5. Program to print sum of first 10 even numbers using while loop
<html>
<body>
<script language="javascript">
var i = 1;
var count = 0;
var sum = 0;
while(count < 10)
{
if(i % 2 == 0)
{
sum = sum + i;
count++;
}
i++;
}
document.write("Sum of first 10 even numbers = " + sum);
</script>
</body>
</html>
Explanation and Working
This program finds first 10 even numbers using while loop. It adds them and prints the total sum.
6. Program using if else if statement
<html>
<body>
<script language="javascript">
var marks = 75;
if(marks >= 80)
{
document.write("Grade A");
}
else if(marks >= 60)
{
document.write("Grade B");
}
else
{
document.write("Grade C");
}
</script>
</body>
</html>
Explanation and Working
This program checks marks and prints grade. Different conditions are checked using else if statement.
7. Program using Switch Statement
<html>
<body>
<script language="javascript">
var day = 2;
switch(day)
{
case 1:
document.write("Monday");
break;
case 2:
document.write("Tuesday");
break;
case 3:
document.write("Wednesday");
break;
default:
document.write("Invalid Day");
}
</script>
</body>
</html>
Explanation and Working
This program uses switch statement. According to value of variable day, it prints the name of the day.
निम्नलिखित प्रोग्राम JavaScript उपयोग करके लिखिए:
- Person vote के लिए eligible है या नहीं
- Adult है या नहीं
- For loop का उपयोग करके पहले 20 numbers print करना
- पहले 10 numbers का sum print करना
- While loop से पहले 10 even numbers का sum print करना
- If else if statement का उपयोग
- Switch statement
उत्तर
1. Vote के लिए eligible है या नहीं
<html>
<body>
<script language="javascript">
var age = 20;
if(age >= 18)
{
document.write("Eligible for Vote");
}
else
{
document.write("Not Eligible for Vote");
}
</script>
</body>
</html>
Explanation और Working
यह program age check करता है। अगर age 18 या उससे ज्यादा है तो Eligible for Vote print होगा, नहीं तो Not Eligible for Vote.
2. Adult है या नहीं
<html>
<body>
<script language="javascript">
var age = 16;
if(age >= 18)
{
document.write("Adult");
}
else
{
document.write("Not Adult");
}
</script>
</body>
</html>
Explanation और Working
यह program check करता है कि person adult है या नहीं।
3. For loop से पहले 20 numbers print करना
<html>
<body>
<script language="javascript">
for(var i=1; i<=20; i++)
{
document.write(i + "<br>");
}
</script>
</body>
</html>
Explanation और Working
यह program 1 से 20 तक numbers print करता है।
4. पहले 10 numbers का sum
<html>
<body>
<script language="javascript">
var sum = 0;
for(var i=1; i<=10; i++)
{
sum = sum + i;
}
document.write("Sum of first 10 numbers = " + sum);
</script>
</body>
</html>
Explanation और Working
यह program 1 से 10 तक numbers को जोड़कर total sum print करता है।
5. While loop से पहले 10 even numbers का sum
<html>
<body>
<script language="javascript">
var i = 1;
var count = 0;
var sum = 0;
while(count < 10)
{
if(i % 2 == 0)
{
sum = sum + i;
count++;
}
i++;
}
document.write("Sum of first 10 even numbers = " + sum);
</script>
</body>
</html>
Explanation और Working
यह program while loop की मदद से पहले 10 even numbers का sum print करता है।
6. If else if statement का उपयोग
<html>
<body>
<script language="javascript">
var marks = 75;
if(marks >= 80)
{
document.write("Grade A");
}
else if(marks >= 60)
{
document.write("Grade B");
}
else
{
document.write("Grade C");
}
</script>
</body>
</html>
Explanation और Working
यह program marks के आधार पर grade print करता है।
7. Switch statement
<html>
<body>
<script language="javascript">
var day = 2;
switch(day)
{
case 1:
document.write("Monday");
break;
case 2:
document.write("Tuesday");
break;
case 3:
document.write("Wednesday");
break;
default:
document.write("Invalid Day");
}
</script>
</body>
</html>
Explanation और Working
यह program switch statement की मदद से day के अनुसार output print करता है।
