Skip to content

Latest commit

 

History

History
2770 lines (1650 loc) · 183 KB

interview-questions-hindi.md

File metadata and controls

2770 lines (1650 loc) · 183 KB

Question 1. What is programming language?

Programming language ek aisa tarika hai jisse hum computers ko instructions dena sikhate hai. Jaise ki hum insaano ke liye languages hote hai, waise hi computers ke liye bhi kuch languages hai jinse unhe samajhne aur follow karne me help milti hai. Programming languages ka use software, applications, websites aur other technologies banane ke liye kiya jata hai.

Question 2. What is front-end?

Front-end woh sab kuch hai jo aap apne phone, computer ya kisi dusre electronic device mein dekhte hai. Yeh uss hisse ko refer karta hai jahan par aapko visual aur interactive elements dikhte hai, jaise ki buttons, menus, text, images, videos, etc. Front-end development mein HTML, CSS aur JavaScript ka istemal kiya jata hai, jo ki web pages aur mobile apps ke liye use kiya jata hai. Iske through, developers visually appealing aur user-friendly interfaces banate hai, jisse users easily interact kar sakte hai.

Question 3. What is back-end?

Back-end ek aisa part hai jiske through website ke data ko store, manage aur process kiya jata hai. Isme servers, databases aur applications hote hai jo ki users ke dwara directly access nahi kiye jaate hai. Back-end developers isme kaam karte hai aur isme programming languages jaise PHP, Python, Java ka use kiya jata hai.

Question 4. What is HTML?

HTML ek markup language hai jo websites banane ke liye use hota hai. HTML ka matlab hota hai "HyperText Markup Language". Iska use kisi bhi webpage ke structure ko define karne ke liye kiya jata hai, jaise ki headings, paragraphs, links, images aur tables wagaira. Iske dwara hum content ko organize, structure aur style kar sakte hain. Basically, agar website ek ghar hai toh HTML uski building blocks hote hain.

Question 5. What is CSS?

CSS ek programming language hai, joki internet ke web pages ka design aur appearance control karta hai. CSS ka matlab hai "Cascading Style Sheets". Isse hum HTML ke content ko khoobsurat banate hai, uski style aur layout ko define karte hai. Hum fonts, colors, borders, images, aur animation jaise elements ko bhi set kar sakte hai. Basically, CSS website ko sundar aur professional look deta hai.

Question 6. What is JavaScript?

JavaScript ek programming language hai jiski madad se websites aur web applications banaye jaate hain. Yah browser mein run hota hai aur user interaction ke sath-sath dynamic content ko bhi handle karta hai. JavaScript ka upyog HTML aur CSS ke saath milakar dynamic web pages banane ke liye kiya jata hai. Iske alawa, JavaScript aajkal desktop aur mobile apps me bhi istemal hoti hai.

Question 7. Difference between java and JavaScript?

Java ek programming language hai jo general-purpose applications ke liye use kiya jaata hai. Ye language object-oriented hai, jismein program ko classes aur objects mein organise kiya jaata hai. Java mein code compile hota hai aur baad mein execute hota hai. Iska use server-side applications, mobile applications, games, etc. ke liye kiya jaata hai.

JavaScript bhi ek programming language hai, lekin ye web development ke liye use kiya jaata hai. Ye client-side scripting language hai, jisse HTML aur CSS ke saath use kiya jaata hai web pages ko interactive banana ke liye. JavaScript ko browser mein run kiya jaata hai, iski madad se user interfaces, animations, form validations, etc. banaye jaate hain.

Iske alawa, Java aur JavaScript mein syntax aur coding approach mein bhi antar hai. Java mein explicit type declarations aur strict syntax hota hai, jabki JavaScript mein simplified syntax aur flexibility hoti hai.

Question 8. Is JavaScript a case-sensitive language?

Haanji, JavaScript ek case-sensitive language hai. Iska matlab hai ki agar aap variables, functions ya keywords ke naam ko capital aur small letters mein alag-alag type karte hain to uska farak padta hai. Jaise ki "apple" aur "Apple" dono alag variables hain JavaScript mein.

Question 9. Tell us about role of HTML, CSS, and JavaScript?

HTML, CSS aur JavaScript teeno ek website banane me important role play karte hain. HTML se website ka content structure banaya jata hai, jaise ki headings, paragraphs, links, images, tables, etc.

CSS ki madad se website ko attractive aur appealing banaya jata hai. Ye styling ke liye use hota hai, jaise ki font size, color, layout, background, borders, etc.

JavaScript website ko interactive banata hai. Ye ek programming language hai jo dynamic web pages banane me help karta hai, jaise ki user input validation, animations, pop-ups, sliders, and other interactive elements.

In teeno ka sahi tarah se istemal karke, ek visually appealing, easy to navigate aur user-friendly website bana sakte hain.

Question 10. Tell us about the history of JavaScript?

JavaScript ka itihaas 1995 mein shuru hua, jab Netscape Navigator browser ke liye ek naya programming language ka avishkar kiya gaya tha. Isse pahle, web pages static aur non-interactive the. JavaScript ne web development ko dynamic aur interactive banaya.

Phir Microsoft Internet Explorer bhi iske saath compatible ho gaya aur tab se yeh programming language web development mein prachalit ho gayi. JavaScript aajkal sabse jyada istemal ki jaane wali programming language hai aur yeh aage bhi bahut hi important rahegi web development ke liye.

Question 11. What is ECMAScript and how is it related to JavaScript?

ECMAScript ek programming language standard hai jiske use se JavaScript ko define kiya jata hai. Ab yeh thoda sa confusing ho sakta hai, lekin suniye - JavaScript ek programming language hai jise web development mein use kiya jata hai. Lekin jab JavaScript banaya gaya tha, tab uske paas koi official standard nahi tha. Isliye, ECMAScript naam ka ek committee banaaya gaya jo JavaScript ke syntax aur semantics ko standardize karne ke liye kaam karti hai. Aaj, jab hum baat karte hain JavaScript ki, toh hum actually ECMAScript standard ki hi baat karte hain jiska latest version ab ES12 (yaani ki ECMAScript 2022) hai.

Question 12. What are the different versions of JavaScript?

JavaScript ke teen mukhya versions hai:

  1. ES5 (ECMAScript 5): Ye JavaScript ka pahla standardized version hai jise 2009 me release kiya gaya tha. Isme kai naye features aur improvements aaye the jaise ki strict mode, array methods, JSON support, etc.

  2. ES6/ES2015 (ECMAScript 2015): Ye JavaScript ka doosra standardized version hai jise 2015 me release kiya gaya tha. Isme bahut sare naye features jaise ki arrow functions, classes, const aur let keywords, modules, template literals, etc. add kiye gaye the.

  3. ES7/ES2016 se lekar ES12/ES2022: In sabhi versions ko samanya taur par "ES Next" ke naam se jana jata hai. Ye versions pratyek varsh ke antaral par release hote hai aur inme regular expressions, async functions, BigInt, optional chaining, nullish coalescing operator, private class fields, top-level await jaise naye features add kiye jate hai.

Iske alawa, browser specific implementation bhi hoti hai jaise Mozilla Firefox ne JavaScript ke liye SpiderMonkey engine develop kiya tha, Google Chrome ke liye V8 engine hai, Safari ke liye Nitro engine hai, aur Microsoft Edge ke liye Chakra engine hai.

Question 13. What is Es5 and Es6?

Es5 aur Es6 dono programming languages hai jo web development mein use hoti hai. Es5 ek puraana version hai aur Es6 naya version hai.

Es5 mein, humein variables ko var keyword se declare karna padta tha, jabki Es6 mein hum let aur const keywords ka use kar sakte hai. Es6 mein arrow functions, template literals, destructuring assignment jaise features available hai jo code likhne ko easy aur efficient banate hai.

Overall, Es6 mein new features add kiye gaye hai jo Es5 ke mukaable developers ko zyada flexibility aur readability dete hai.

Question 14. What are the different places where we can write JavaScript code?

JavaScript code likhne ke alag-alag jagah hain, jaise:

  1. Browser Console - Agar aap kisi website ko open karke usme JavaScript code chalana chahte hain to aap browser console ka istemaal kar sakte hain. Ismein code type karke "enter" dabane se code execute ho jayega.

  2. Text Editor - Agar aap JavaScript ka code edit karne chahte hain to aap text editor jaise ki Notepad, Sublime Text, Atom ya Visual Studio Code ka istemaal kar sakte hain. Ismein code likhkar .js extension ke saath save kar sakte hain aur phir web page mein link kar sakte hain.

  3. Integrated Development Environment (IDE) - IDE jaise ki Visual Studio Code, WebStorm, ya Eclipse ka istemaal karke aap JavaScript code likhna aur debug karna bhi kar sakte hain. IDEs mein syntax highlighting, error checking aur debugging features hote hain jo coding process ko aasan banate hain.

  4. Online Code Editors - Jaise ki CodePen, JSFiddle, ya Repl.it jaisi website aapko online JavaScript code editor provide karti hai, jismein aap code likh aur run kar sakte hain. Ye websites demo projects banana ke liye bahut upyogi hote hain.

Uparokt jagahon ka upyog karke ham JavaScript code likh sakte hain aur iska upyog karke hum dynamic web pages aur applications develop kar sakte hain.

Question 15. Difference between client side and server side JavaScript?

Client-side JavaScript khud ke web browser mein chalta hai aur yeh interactive web pages banane ke liye use hota hai. Yeh content ko refresh kiye bina dynamic tareeke se update karne ki anumati deta hai.

Server-side JavaScript server par chalta hai aur user input ko handle karne, database operations ke liye aur dynamic content ko generate karne jaise kaam ke liye use hota hai jo client tak bheja jaata hai.

Question 16. Explain all the JavaScript features in detail?

JavaScript ek programming language hai jo web development mein use hota hai. Ye browser-based language hai jisse HTML aur CSS ke saath milakar web pages ko interactive banaya ja sakta hai.

JavaScript ki kuch important features niche diye gaye hain:

  1. Variables: JavaScript mein variables ka use values store karne ke liye kiya jata hai. Variables declare karne ke liye var, let, ya const keyword ka use kiya jata hai.

  2. Functions: Functions ek set of statements hote hai, jo ek baar define hone ke baad bahut baar reuse kiye jaa sakte hain. Functions parameters aur return values bhi le sakte hain.

  3. Objects: Objects ek collection of properties hote hai, jinke values ke saath kaam kiya jata hai. Properties keys ko unique hona chahiye.

  4. Arrays: Arrays ek list ke form mein values store karte hain. Inme multiple data types ke values add ho sakte hain. Arrays ke elements index ke through access kiye jate hain.

  5. Conditional Statements: JavaScript mein conditional statements jaise if, else, aur switch ka use kiya jata hai. Ye statements code ko control karte hain aur aage ki execution decide karte hain.

  6. Loops: Loops, jaise for aur while, ka use iterative tasks ke liye kiya jata hai. Ye statements ek condition ko check karte hain aur execution ko control karte hain.

  7. DOM Manipulation: Document Object Model (DOM) ka use JavaScript se kisi bhi HTML element ko interact karne ke liye kiya jata hai. Isse hum apne web page ko dynamic banate hain.

  8. Events: JavaScript events user actions jaise clicks, keystrokes, ya form submissions ke saath work karte hain. Ye events use karke hum apne web pages ko interactive banate hain.

In sabhi features ka use karke, JavaScript developers web pages ko dynamic aur interactive bana sakte hain.

Question 18. Is javascript a statically typed or a dynamically type language?

Javascript ek dynamically typed language hai. Yeh matlab hai ki humko code likhte waqt variables ko data type define nahi karna padhta hai aur runtime pe hi determine hota hai ki us variable mein kis type ka data store hoga.

Question 19. What is Single Threaded?

Single threaded ka matlab hai ki ek hi thread se kaam hota hai. Ek thread ek saath ek hi kaam ko karta hai aur doosre kaamo ke liye intezaar karta hai. Iska matlab yeh hai ki agar koi kaam chal raha hai aur usmein kuchh der tak dikkat aa jaye to doosre kaam ko shuru karne ke liye hamen pahle pehle wale kaam ka intezaar karna hoga. Isse multitasking me dikkat ho sakti hai kyunki ek hi waqt me sirf ek hi kaam ho sakta hai.

Question 20. What is Non-Blocking Event Loop?

Non-Blocking Event Loop ek aisa tarika hai jismein code ek loop dwara chalaya jaata hai jisme koi blocking nahi hota. Yeh aam taur par server-side programming mei use kiya jaata hai jahan bahut saare clients se communication ki zarurat hoti hai. Ismein, jab koi client code execute karta hai, toh uska execution control immediately return ho jaata hai aur doosre clients ke liye bhi processing shuru kar di jaati hai. Jab koi client ka processing complete ho jaata hai, event loop us client ko notify karta hai aur uss client ke code ko execute karne ke liye ready ho jaata hai. Is tareeke se non-blocking I/O operations perform kiye jaate hai jo CPU resources ko optimize karte hai.

Question 21. What is value and variable?

Value ek particular quantity ya quality hai jaise ki 5, "hello", True. Variable ek naam hota hai jise hum value store karne ke liye use karte hain, jaise ki x, name, age. Hum variable mein value assign kar sakte hain aur baad mein uss value ko access kar sakte hain. For example, agar humne x=10 assign kiya to hum x ki value 10 ke equal manenge.

Question 22. What are rules of defining a variable?

Variable define karne ke kuch niyam hai:

  1. Pehle variable ka naam decide karo, jaise ki "num_students".
  2. Ab uss variable ko ek data type assign karo, jaise ki "int" ya "float".
  3. Variable ko value assign karo, jaise ki "num_students = 25".
  4. Agar aap multiple variables define karna chahte hai, to unko comma se separate karo, jaise ki "num_students, avg_marks, class_teacher".
  5. Variable naam alphabets aur underscores (_) se shuru hona chahiye, numbers se nahi.
  6. Variable naam case-sensitive hota hai, matlab "num_students" aur "Num_Students" alag-alag variables hai.

Question 23. What is data type?

Data type ek tarah ka variable hai jiska use hum karte hain data ko store karne ke liye. Har ek variable ki apni ek data type hoti hai, jaise numbers ke liye "int" (integer) data type hota hai aur words ke liye "str" (string) data type hota hai. Data types batate hain ki variable mein kis tarah ka data stored hoga aur uspe konse operations possible honge.

Question 24. What are the different types of data types ?

JavaScript mein kuch alag-alag data types hote hai, jaise:

  1. String: Ye ek sequence of characters hota hai jo single quote '' ya double quote "" ke beech mein hota hai.

  2. Number: Ye numerical values ko represent karta hai, jaise 2, 3.14, -10, etc.

  3. Boolean: Ye true aur false value ko represent karta hai.

  4. Undefined: Jab koi variable declare kiya jata hai lekin usme koi value assign nahi ki gayi hoti hai to wo undefined hota hai.

  5. Null: Ye koi value na hone ka matlab hota hai.

  6. Object: JavaScript mein object ek collection of properties aur unke values hota hai. Ye key-value pairs ki tarah hota hai.

  7. Array: Ye bhi ek type of object hai jo multiple values ko store karne ke liye use hota hai. Ismein values ko index ki madad se access kiya jata hai.

  8. Function: Ye ek block of code hota hai jo kisi specific task ko perform karta hai.

Question 25. What is Symbol ?

JavaScript mein "Symbol" ek data type hai jo unique aur immutable hota hai. Yeh primitive data type hai jise create karne ke liye hum Symbol() function ka use karte hain.

JavaScript mein "Symbol" ek tarah ka data type hai jo bilkul alag aur badalne ke liye anivarya (immutable) hota hai. Iska matlab hai ki ek baar jab hum isko banate hain toh usse hum badal nahi sakte. Ise banane ke liye hum Symbol() naam ka ek function istemaal karte hain.

Question 26. What is BigInt ?

BigInt JavaScript mein ek data type hai jo ki bahut bade numbers ko represent karne ke liye use kiya jata hai. Iska matlab hai ki aap ismein bahut bade numbers assign kar sakte hain (jo normal integer data type mein nahi ho sakte).

Iss data type mein number ke end mein "n" lagana hota hai, jisse ye pata chalta hai ki ye BigInt hai. Jaise ki const myNumber = 123456789012345678901234567890n;

Isse, aap bahut bade calculations kar sakte hain aur precision ko maintain kar sakte hain. Jabki normal integers mein, agar number limit se jyada bada ho jaye toh uska result unexpected ho sakta hai.

Question 27. What is the difference between primitive and non-primitive data types ?

JavaScript mein primitive aur non-primitive data types ka fark hai. Primitive data types like number, string, boolean, undefined, aur null hai jo ki simple values hote hai aur ek hi memory space ko occupy karte hai. Non-primitive data types like arrays aur objects hai, jo multiple values ya properties ka collection hai aur alag-alag memory spaces ko occupy karte hai. Ek primitive data type ki value change nahi ki ja sakti hai lekin non-primitive data type ki values changeable hoti hai.

Question 28. Why is the typeof NaN ?

NaN ka type javascript me "number" hota hai. Lekin NaN ek special number hai jo aisi situations me generate hota hai jab kuch mathematical operation undefined ya impossible ho jata hai, jaise ki 0/0 ya Infinity - Infinity. Iska matlab yeh nahi ki NaN ek sahi number hai, balki yeh indicate karta hai ki koi calculation galat ho gaya hai.

Question 29. What is the typeof null ?

JavaScript में null का टाइप object होता है। यह एक special primitive value है जो कि अपने आप में empty या non-existent object को दर्शाता है। जब भी आपको एक खाली या non-existent object चाहिए, तो आप null का उपयोग कर सकते हैं।

Question 30. What is comment and how to add a single line or multi line comment in JavaScript?

Comment ek note hota hai jo code ke saath juda hota hai aur code aur uske kaam ki jaankari ko batata hai. Comment execute karne par computer dwara ignore kiya jata hai.

Ek single line comment ko JavaScript me add karne ke liye, line ke shuru me // (double forward slash) ka upyog karen, phir apna comment likhen. Jaise ki:

// Ye ek single line comment hai

Ek multi-line comment ko JavaScript me lagane ke liye, comment block shuru karne ke liye /* (slash asterisk) ka upyog karen, aur ise khatm karne ke liye */ (asterisk slash) ka upyog kare. Jaise ki:

/*
Ye ek multi-line comment hai.
Isme kai lines shamil ho sakti hain.
*/

Question 31. What is Infinity ?

Infinity ek number hai jo Javascript mein "Infinity" ke naam se represent kiya jata hai. Ye ek special value hai jo bataata hai ki koi bhi number isse bada hoga ya nahi. Iska matlab hai ki agar hum kisi variable ko Infinity ke equal set karte hain, to wo variable kabhi bhi infinity se bada nahi ho sakta.

Jaise ki agar hum ye code likhte hain:

let a = Infinity;
console.log(a + 1);

Toh iska output hoga Infinity hi kyunki koi bhi number infinity se bada nahi ho sakta.

Iske opposite value bhi hai, jo -Infinity ke naam se jaani jaati hai aur ye bhi same tareeke se kaam karti hai.

Question 32. What is the significance of use strict ?

"use strict" in Javascript is a way to enable strict mode, which helps to write safer and more efficient code. It restricts certain actions that are considered unsafe or deprecated, and throws errors instead of allowing them to occur silently.

Strict mode enforces stricter rules for variable declaration, prevents the use of undeclared variables, and disables some confusing features like automatically creating global variables. This can help prevent common programming mistakes and make code easier to debug.

We can say that "use strict" in Javascript is like an alarm that helps to prevent common mistakes while writing code. It provides stricter rules and prevents the use of unsafe or confusing features, making code safer and easier to read and debug.

Question 33. How JavaScript behave in non-strict mode?

Non-strict mode mein, JavaScript apni rules ko thoda kam strict follow karta hai. Iska matlab yeh hai ki non-strict mode mein aapko kuch flexibility mil jaati hai, jaise ki undefined variables ka use kar paana ya phir duplicate property names ka use karna.

Iss mode mein, agar aap kisi variable ko bina usse pahle declare kiye use karte hai toh wo global variable ban jaata hai. Iske alawa, iss mode mein functions ko call karne ke liye unhe pahle define karne ki zaroorat nahi hoti hai.

Overall, non-strict mode se JavaScript ka behavior thoda kam predictable ho jaata hai aur chote-chote mistakes ke chances badh jaate hai.

Question 34. What is a statement in programming?

Programming mein, ek statement ek code line hoti hai jo computer ko execute karne ke liye boli jaati hai. Yeh ek command hoti hai jo kuch karne ke liye boli jaati hai, jaise ki variable assign karna, calculation karna, ya phir condition check karna. Har ek statement apni syntax aur meaning ke hisab se likhi jaati hai. Statement ka use program banane ke liye bahut important hota hai kyunki yeh program ke structure aur flow ko define karta hai.

Question 35. How to write a single line of statement?

Ek line ka statement likhne ke liye, aapko apni baat ko ek sentence mein sum up karna hoga. Iske liye aapko apni language ke grammar aur vocabulary ka sahi istemal karna hoga. Yaad rakhein ki ek line ka statement chota aur prabhavi hona chahiye. Agar aapka statement lamba hai, toh aap usmein zaroori points ko highlight karein. Yeh ek accha tarika hai apna point convey karne ka ek dum seedha-meda tareeka!

Question 36. How to write a multi-line statement?

Multiline statement likhne ke liye aapko ek line mein complete statement likhne ki jagah, usse kuch alag alag lines mein break karna hoga. Iske liye aapka language ka compiler ya interpreter kuch specific symbols recognize karega jaise \ ya {} ya (). In symbols ko use karke aap multiline statements likh sakte hain.

Maan lo ki aap ek function define kar rahe ho aur uske arguments bahut lambi list hai. Toh aap uss statement ko multiple lines mein likh sakte hain:

def function_name(argument1,
                  argument2,
                  argument3):
    # Function body here

Ismein aapne , comma ke baad enter lagakar agli line pe arguments likhe hain. Aap is tarah se kisi bhi bade statement ko multiple lines mein divide kar sakte hain.

Question 37. What is a code block?

Code block ek group hai jisme programming statements ko group kiya jata hai. Ye statements kisi specific task ya operation ko complete karne ke liye use kiye jate hai. Code block brackets {} ke beech me likhe jaate hain aur ek sath execute kiye jate hain.

Question 38. What is let, const and var ?

let, const aur var JavaScript mein variables ko declare karne ke liye istemal kiye jaate hain.

var ek purana tareeka hai variables ko declare karne ka, yeh variables kee value ko badalne ki anumati deta hai aur undefined values ko bhi accept karta hai.

let aur const naye tareekon hai variables ko declare karne ke liye. let variables ko update karne ki anumati deta hai, jabki const variables ko ek baar hi assign kiya ja sakta hai aur phir use change nahi kiya ja sakta hai.

Yaad rakhen, agar aap variable ko update nahi karna chahte toh const ka upyog karen, var se bachen aur let ko tabhi istemal karen jab aapko variable ki maan ko change karne ki zaroorat ho.

Question 39. Difference between let, const and var ?

JavaScript me "let", "const" aur "var" teen tarah ke variables hai.

  1. "var" ko pahle se hi JavaScript me use kiya jaata tha. ye purane samay ke programming languages ki tarah hai. iska upyog variable ke liye kar sakte hai, lekin ye local aur global dono level pe ho sakta hai.

  2. "let" ek naya variable hai jo ES6 (ECMAScript 2015) me introduce hua hai. ye var se thoda alag hai kyunki ye variable ko block level scope provide karta hai. matlab ki agar aap kisi function ya loop ke andar let ka use karte hai to vo bahar se access nahi ho sakta.

  3. "const" bhi ES6 me introduce hua hai. Jaisa ki naam se pata chalta hai "const" constant variable hai. Matlab ki jab ise ek baar define kar diya jaye to uski value change nahi ki ja sakti. Const bhi let ki tarah block level scope provide karta hai.

Toh yeh thi JavaScript me let, const aur var ke bich antar. Var mai hum variable ko declare kar sakte hai, let mai variable ko block level scope milta hai aur const mai variable ki value ko change nahi kiya ja sakta.

Question 40. When to use let, const and var ?

JavaScript mein let, const aur var tino keywords variables ko declare karne ke liye use kiye jaate hain. Lekin in teeno mein antar hai.

var variable ko function level scope deti hai, jabki let aur const block level scope dete hain. Iska matlab hai ki var se declare kiya gaya variable us function ke bahar bhi accessible rahega, lekin let aur const se declare kiya gaya variable sirf un block ke andar hi accessible rahega, jismein woh declare kiya gaya hai.

let aur const dono hi block level scope dete hain, lekin const ek immutable variable hota hai, yani ki uska value change nahi kiya ja sakta hai, jabki let ka value change kiya jaa sakta hai.

Aise mein, agar aapko variable ki value badalni nahi hai to aap const ka use karein aur agar value badalni hai to let ka use karein. var ka use avoid karein kyunki yeh global scope mein declare ho jaata hai aur isse unintended behavior aane ke chances badh jaate hain.

Question 41. What is ++, -- operators ?

++ and -- are called increment and decrement operators in Javascript. Jab hum kisi variable pe ++ ya -- lagaate hai toh uski value ek se badh jaati hai (increment) ya ek se ghat jaati hai (decrement). For example, agar humare paas ek variable x hai jiske value 3 hai aur hum x++ likhte hai toh uska value ab 4 ho jaayega. Similarly, agar hum x-- likhte hai toh uska value 2 ho jaayega. Ye operators generally loops mein use kiye jaate hai jahan hume kuch specific number of times loop ko chalana hota hai.

Question 42. What is typeof operator ?

typeof operator JavaScript mein ek built-in operator hai jo humein kisi value ka data type pata karne mein madad karta hai. Is operator ka use karke hum chahe toh apne code mein kisi variable, function ya object ki data type ko check kar sakte hain.

Iss operator ke through humare paas 7 types ke results aa sakte hain: "number", "string", "boolean", "undefined", "object", "function" aur "symbol". Agar koi value null hai, to typeof operator usse object samajhta hai jo ek bug hai JavaScript ka.

In summary, typeof operator JavaScript mein ek bahut hee useful operator hai jiske through hum kisi bhi value ka data type aasani se pata kar sakte hain.

Question 43. What is reminder operator ?

Reminder operator JavaScript mein ek arithmetic operator hai jo "%" (percent) ke symbol se represent kiya jata hai. Ye operator do numbers divide karne par remainder (bacha hua hissa) ko return karta hai. Jaise, 7 % 3 ka answer 1 hoga kyunki jab hum 7 ko 3 se divide karte hain toh bache hue 1 hai. Reminder operator JavaScript mein do numbers ko divide karne par unke bacha hua hisse ko deta hai.

Question 44. Explain logical &&, || and ! operators

"Logical &&" (aur), "||" (ya) aur "!" (na) operators JavaScript mein hote hai.

"&&" operator ek logical "AND" operator hai, jo do conditions ke beech mein use kiya jaata hai. Agar dono conditions true hai toh uska result true hota hai, warna false hota hai. For example:

if (age > 18 && name === 'John') {
  // some code
}

Is example mein, agar age 18 se bada hai aur naam John hai, tabhi ye condition true hoga.

"||" operator ek logical "OR" operator hai, jo do conditions ke beech mein use kiya jaata hai. Agar koi bhi ek condition true hai toh uska result true hota hai, warna false hota hai. For example:

if (day === 'Saturday' || day === 'Sunday') {
  // some code
}

Is example mein, agar din Saturday ya Sunday hai, tabhi ye condition true hoga.

"!" operator ek unary logical operator hai, jo operand ke opposite value return karta hai. Agar operand true hai toh ! operand false return karega, aur agar operand false hai toh ! operand true return karega. For example:

var isLogged = false;
if (!isLogged) {
  // some code
}

Is example mein, variable isLogged false hai, lekin kyunki humne usko ! ke saath use kiya hai, condition true ho jayegi.

Question 45. What are the different ways of writing output in JavaScript?

JavaScript me output likhne ke alag-alag tarike hai jaise -

  1. Console.log() - isse hum output ko console me print kar sakte hai, jisse debugging kiya ja sakta hai

  2. Document.write() - isse hum output ko web page par direct write kar sakte hai, lekin yeh ab jyada use nahi hota

  3. Window.alert() - isse hum ek pop-up alert message show kar sakte hai, lekin yeh bhi ab kam use hota

  4. InnerHTML - isse hum element ke andar content dal sakte hai, jaise ki kisi div tag ke andar text ya HTML code.

In sabhi tarike se output display karne ke liye JavaScript ka use kiya jata hai.

Question 46. What is a string ?

Javascript mein "string" ek data type hai jo kisi bhi text ya characters ki sequence ko represent karta hai. Jaise ki naam, address aur message ke liye strings ka use hota hai. Ye quotes (" ") ya apostrophe (' ') mein define kiya jaata hai.

Question 47. How to create a String ?

String ek data type hai jiski madad se hum text ko represent kar sakte hain JavaScript mein. String ko create karne ke liye, aapko bas double quotes (" ") ya single quotes (' ') ke beech mein text likhna hai, jaise "Hello World" ya 'My name is John'. Yeh string literals hote hain aur wo khud hi ek string variable mein store ho jaate hain. Aap chahe to string variable ko bhi define kar sakte hain, jaise var message = "Welcome to my website".

Question 48. Difference between string Literal vs string object ?

String literal aur string object dono JavaScript mein strings ko represent karne ke liye istemal kiye jate hain.

String literal ek tarah ka constant hota hai jo code mein hard-coded hota hai, jaise "Hello" ya 'World'. Ye kabhi bhi change nahi ho sakta hai.

Wahi string object variable ki tarah hota hai, jise hum string class se create karte hain. Ye dynamic hota hai, matlab ki ismein hum value ko kabhi bhi change kar sakte hain.

Jaise ki hum ye code likh sakte hain:

var str1 = "Hello"; // String literal
var str2 = new String("World"); // String object

Ismein str1 ek string literal hai aur str2 ek string object hai. Hum str2 mein kuch changes kar sakte hain, lekin str1 ki value hum kabhi nahi badal sakte.

Question 49. What is string length property ?

JavaScript mein "length" ki string property se ek string ke characters ki sankhya nikali jaati hai. Is property se space aur special characters ke saath saath sabhi characters ko count kiya jaata hai. Aap is property ko "myString.length" ke dwara use kar sakte hain.

Question 50. How to convert string to array ?

Javascript mein string ko array mein convert karna bahut hi aasan hai. Aapko bas string ke upar split() method ka use karna hoga. Is method ke dwara aap string ko ek delimiter (jaise comma, space, etc.) ke basis par multiple pieces mein split kar sakte hain aur fir unhe array mein store kar sakte hain.

Yahaan ek simple example diya gaya hai:

var str = "Hello World";
var arr = str.split(" "); // yahaan humne space delimiter ka use kiya hai
console.log(arr); // output: ["Hello", "World"]

Is example mein humne split() method ka use kiya hai string "Hello World" ke upar aur usse space delimiter par split kiya hai. Fir jo pieces milte hain unhe ek array mein store kar diya hai.

Question 51. What is String Template Literal ?

String Template Literal Javascript mein ek tareeka hai jisme hum backticks (``) ka istemaal karke variables, expressions aur strings ko asan tarike se combine kar sakte hai. Ismein placeholders ($) ka use kiya jaata hai jiske andar variable ya expression ki value replace ki jaati hai. Ye hume traditional string concatenation se bachata hai aur code ko padhne aur likhne mein suvidha deta hai.

Question 52. Difference between String Literal and String template literal ?

String Literal aur String Template Literal dono tarah ke Strings hai jo JavaScript ko prabhavit karte hain.

String Literal ek simple string hota hai jiske liye hum single quotes ('') ya double quotes ("") ka upyog karte hain. Jaise ki:

const name = 'John';

String Template Literal ek aisa string hai jismein hum variables, expressions aur functions ko include kar sakte hain, uske liye hum backticks (`) ka upyog karte hain. Jaise ki:

const age = 25;
console.log(`My age is ${age}`);

Ismein ${} enclosed expressions hai jo variable ya expression ke value ko include karta hai.

Toh mukhya antar hai ki String Literal mein hum variables aur expressions ko include nahi kar sakte hai jabki String Template Literal mein hum ye sab include kar sakte hai.

Question 53. Difference between slice, substring and substr functions ?

Slice, substring aur substr functions JavaScript mein string ke kuchh hisso ko alag tarike se laane ke liye istemaal kiye jaate hain.

slice() function ek string ka ek nirdharit hissa (slice) return karta hai. Ismein do arguments diye jaate hain - start aur end index. Yadi end index diya nahin gaya hai to slice end tak chalta hai.

substring() function bhi ek string ka nirdharit hissa return karta hai, lekin ismein start aur end dono indexes diye jaate hain. Yadi end index diya nahin gaya hai to substring end tak chalta hai.

substr() function ek string ke nirdharit shuruvaat index se shuru hokar ek nirdharit lambai tak ka hissa return karta hai. Ismein do arguments diye jaate hain - start index aur length.

Is tarah se, slice(), substring() aur substr() functions string manipulation mein kaafi upyogi hote hain aur inhe sahi tarike se samajhna jaroori hai.

Question 54. What is the difference between exec and test function in JavaScript?

Exec function aur Test function dono JavaScript ke regular expression functions hai. Ye dono string pattern matching me use kiye jate hai.

Exec function ek string ko input ke roop me leta hai, usme se ek match dhoondta hai, aur phir us match ke saare details ko ek array ke andar return karta hai. Agar koi match nahi milta toh null return hota hai.

Wahi test function bhi ek string ko input ke roop me leta hai, aur usme diye gaye regular expression ka ek match dhoondta hai. Par is case me, yeh function sirf true ya false return karta hai, depending on whether the given string matches the given regular expression or not.

In short, exec function detailed information provide karta hai aur test function boolean value (true or false) return karta hai.

Question 55. What is the difference between padStart and padEnd functions ?

padStart aur padEnd dono functions JavaScript mein string ko modify karne ke liye use kiye jaate hain.

padStart function ek string ki shuruwat mein naye characters ko add karta hai, jabki padEnd function uski ant mein characters ko add karta hai. Dono functions mein 2 arguments hote hain: length (string ki final length) aur character (jo bhi character add karna hai).

For example:

const str = "123";
console.log(str.padStart(5, '0')); // output: "00123"
console.log(str.padEnd(5, '0')); // output: "12300"

Yahan humne padStart ka use kiya aur 2 characters '0' add kiye string ke shuruwat mein jisse string ki final length 5 ho gayi. Aur padEnd ka use kiya aur 2 characters '0' add kiye string ke ant mein jisse string ki final length phir bhi 5 hi rahi.

Is tarah se, padStart aur padEnd dono functions string modification mein use kiye jaate hain lekin unka difference yeh hai ki padStart function shuruwat mein characters add karta hai aur padEnd function ant mein characters add karta hai.

Question 56. What is the difference between indexOf and includes ?

JavaScript mein, 'indexOf' aur 'includes' dono string ke methods hai jo ek string mein kuch dhoondne ke liye use kiye jaate hain.

'indexOf' method ek string ko check karta hai aur usmein di hui value ya substring ka index (position) deta hai. Agar string mein value nahi hoti toh '-1' return karta hai.

Example:

let str = "Hello World";
console.log(str.indexOf("World")); // Output: 6
console.log(str.indexOf("India")); // Output: -1

While 'includes' method ek boolean value return karta hai, jismein true return hota hai agar value substring mein present ho aur false return hota hai agar value substring mein absent ho.

Example:

let str = "Hello World";
console.log(str.includes("World")); // Output: true
console.log(str.includes("India")); // Output: false

In summary, 'indexOf' returns the index of a given substring in a string, whereas 'includes' returns a boolean value indicating whether or not the string contains the specified substring.

Question 57. What is the difference between indexOf and lastIndexOf function ?

indexOf aur lastIndexOf dono JavaScript me string par kaam karne wale functions hain. indexOf function ek string me se kisi specific character ya substring ka first occurrence dhundhta hai, jabki lastIndexOf function ek string me se kisi specific character ya substring ka last occurrence dhundhta hai.

Matalab, agar hum ek string ke andar se kisi character ya substring ka first occurrence dhundhna chahte hain to hum indexOf ka use karenge, jahan tak ki agar humein ek string ke andar se kisi character ya substring ka last occurrence dhundhna hai to hum lastIndexOf ka use karenge.

Question 58. What is the difference between search and match functions ?

Search aur match functions dono JavaScript mai string ke sath kaam karte hai. Lekin dono functions ke kaam me thoda sa difference hai.

Search function ka use hum kisi particular word ya character ko kisi string me search karne ke liye karte hai. Isme hum apne search query ko string ke sath compare karte hai aur first occurence jaha wo milta hai uski index value return kar deta hai. Is function ka syntax hai:

string.search(searchvalue)

Wahi match function ka use hum ek string ke ander se kisi specific pattern ko find karne ke liye karte hai. Isme hum pattern ko regular expression ke sath compare karte hai aur agar wo milta hai to wo match kiya jata hai. Is function ka syntax hai:

string.match(regexp)

Toh in summary, search function kisi word ya character ki position find karne ke liye use hota hai jabki match function kisi specific pattern ko find karne ke liye use hota hai.

Question 59. What is Implicit type conversion ?

Implicit type conversion ka matlab hai ki Javascript mein ek value ke data type ko apne aap dusre data type mein convert kar diya jata hai bina ki programmer ke explicit tarike se specify kiye jaane. Iska matlab hai ki agar aap ek number aur string ko add karte hain jaise ki 3 + "5", toh string "5" ko implicitly number mein convert kar diya jayega aur result 8 hoga. Yah us waqt hota hai jab JavaScript expression ko samajhne ki koshish karta hai aur jarurat padne par data types ko convert karta hai.

Question 60. What is Explicit type conversion ?

Explicit type conversion in Javascript is the process of converting a value from one data type to another using explicit methods like Number(), String(), Boolean(), etc. This allows you to manipulate and use the value in a way that is appropriate for its new data type.

Isme hum javascript mein ek value ko dusre data type me badalne ka process kehte hai, aur iske liye hume tay kiye gaye method jaise Number(), String(), Boolean() ka use karna hota hai. Isse hum us value ko apne naye data type ke hisab se use kar sakte hai.

Question 61. What is Coercion ?

Coercion ek concept hai jisme Javascript automatic tarike se datatypes ko convert karta hai. Jab hum operators, functions ya comparison statements ka use karte hain toh kabhi kabhi unke operands ke datatype alag hote hain, isliye JavaScript coercion ka use karta hai taki unhe sahi tarike se compare ya operate kar sake. Coercion ke do types hote hain: implicit aur explicit. Implicit coercion hota hai jab JavaScript khud hi ek datatype ko dusre datatype mein convert karta hai, jabki explicit coercion mein hum apne code mein datatype conversion karte hain.

Question 62. What are different popup boxes available in core JavaScript?

JavaScript mein kuch aise popup boxes hote hain jo website developers use karte hain aur user se input ya information collect karne ke liye. Yeh hai kuch popular popup boxes:

  1. Alert box - Isme ek message display hota hai jisse user ko koi important information di jaati hai.

  2. Prompt box - Isme user se ek question poocha jaata hai aur uska response collect kiya jaata hai.

  3. Confirm box - Isme user se Yes/No ke options provide kiye jaate hain jisse user apni choice select kar sakta hai.

In sabhi popup boxes mein hum message display kar sakte hain aur user se interaction kar sakte hain.

Question 63. What are the truthy and falsy values in JavaScript?

JavaScript mein truthy values voh hai jo 'true' ya non-zero ya non-empty mana jaate hai. Examples mein include koi bhi non-empty string, non-zero number, object, array, aadi.

Dusra taraf, falsy values voh hai jo 'false' ya zero ya empty mana jaate hai. Examples mein shamil hai ek khali string (''), 0, null, undefined, NaN, aur false.

Question 64. Give examples of falsy values ?

JavaScript mein kuch values hai jo ki false hote hai, unhe hum falsy values kehte hai. Yeh values hote hai:

  1. False - jaisa ki naam se hi pata chalta hai, yeh value false hoti hai.

  2. 0 - 0 bhi ek falsy value hai kyunki yeh numeric zero hai.

  3. Empty string ("") - jab koi bhi string empty hoti hai, tab yeh falsy value ho jaati hai.

  4. Null - null ek special keyword hai, jo ki ek falsy value hai.

  5. Undefined - undefined bhi ek falsy value hai, jaise ki variable ko declare kiye bina use karna.

  6. NaN - NaN ka matlab "Not a Number" hai, jab hum kisi invalid arithmetic operation ko perform karte hai, toh yeh value aati hai aur yeh bhi ek falsy value hai.

In sabhi values ko agar hum condition mein use karte hai, toh woh false return karenge.

Question 65. What is Loose equality operator ?

The loose equality operator in JavaScript is denoted by "==" and it compares two values for equality, but it does not strictly compare their data types. In other words, it performs type coercion before comparing the values.

For example, 10 == '10' will return true because the values are equal after type coercion (the string '10' is converted into the number 10).

However, it is generally recommended to use the strict equality operator "===", which compares both value and data type without performing type coercion. This can help avoid unexpected behavior in your code.

Question 66. What is Strict equality operator ?

JavaScript mein strict equality operator teen equal signs '===' se represent kiya jata hai, aur yeh check karta hai ki do values nahi sirf value mein equal hai, balki type mein bhi equal hai ya nahi. Agar do values ke types alag hai toh strict equality operator false return karega.

Question 67. Difference between == and === ?

== and === are both used for comparison in JavaScript, but with a slight difference.

== compares the values of two variables, but it doesn't check their data types. This means that if the values are the same, then it will return true even if they are different data types.

On the other hand, === not only compares the values of two variables but also checks their data types. So, if the values of two variables are the same and their data types are also the same, then it will return true.

For example, "5" == 5 will return true because both have the same value, but "5" === 5 will return false because they are different data types (string and number).

To put it simply, == is like comparing apples to oranges without considering their types, while === is like comparing apples to apples and oranges to oranges.

Question 68. Difference between while and do while loop ?

While aur do while loop dono hi JavaScript mein loop banane ke liye use kiye jaate hain. Ek while loop mein, pehle se hi condition check hoti hai, phir loop chalta hai ya nahi chalta hai uski decision leti hai. Jabki do while loop mein sabse pehle loop ek baar chalta hai aur tabhi condition check hoti hai.

Iska matlab hai ki agar aapko kam se kam ek baar to loop chalana hai, chahe condition true ho ya false ho, to aap do while loop ka upyog kar sakte hain. Lekin agar aapko pahle se hi condition check karna hai aur phir loop ke andar jana hai ya nahi iska faisla karna hai, to aap while loop ka upyog karenge.

Question 69. What is for of loop ?

For of loop javascript mein ek tarah ka loop hai jo iterable objects jaise arrays, strings, maps ya sets mein iterate karne ke liye istemal kiya jata hai. Ye aapko object ke har item se loop karne ki anumati deta hai bina index number ki chinta kiye.

Question 70. What is for in loop ?

For loop JavaScript mein ek tarah ka loop hai jiske dwara hum kuch code ko bar-bar ek se adhik baar chala sakte hain. For loop ke andar teen expressions hote hain - initialization, condition, aur increment/decrement. Initialization mein hum variable ki value set karte hain jiska use hum loop ke andar karenge. Condition mein hum ye decide karte hain ki kitni baar loop ko chalana hai. Aur increment/decrement mein hum variable ki value ko badhate ya ghatate hain.

For loop ki madad se hum ek array ke sabhi items ya ek range of numbers ko easily iterate kar sakte hain. For example, agar hume ek array ke saare items ko print karna hai to hum for loop ka use kar sakte hain:

var arr = [1, 2, 3, 4, 5];

for (var i = 0; i < arr.length; i++) {
   console.log(arr[i]);
}

Is code mein humne arr naam ka ek array banaya hai jismein 1 se lekar 5 tak ke numbers hain. Fir humne for loop ka use karke i variable ki starting value 0 set ki hai. Condition mein humne check kiya hai ki i variable array ke length se kam hai. Increment statement mein humne i variable ki value ko 1 se increase kiya hai. Loop ke har iteration mein hum console.log() function ka use karke array ke ek item ko print kar rahe hain.

In short, for loop ek powerful tool hai JavaScript programming mein jiske dwara code ko repeat karne aur iterating tasks ko aasan tarike se solve karne ke liye istemaal kiya jaata hai.

Question 71. What is Switch ?

Switch JavaScript mein ek statement hai jo multiple conditions ko check karne ke liye use kiya jaata hai. Hum switch ka upyog tab karte hain jab hume ek variable ke alag-alag values ke according alag-alag actions perform karne hote hain.

Ismein hum ek variable ke value ko check karte hain aur uske hisaab se uss value ke corresponding action execute karte hain. Agar koi bhi value match nahi hoti hai toh default action execute hota hai.

Question 72. What are cases in switch ?

Switch statement ek aisa control flow statement hai jo ek variable ke value ke according multiple block of codes ko execute karne ki permission deta hai. Har block ko ek case keyword se define kiya jata hai, jisme variable ki value check ki jati hai. Agar variable ki value case ke value ke barabar hoti hai to uss block ka code execute ho jata hai.

Imagine a traffic signal and each signal has three lights - red, green, and yellow. The traffic signal controller is like a switch statement and the different signal lights are like the cases. Depending on the value of the signal light (the variable), the corresponding block of code (the traffic rules for that particular light) will be executed.

Question 73. What is default case ?

Default case JavaScript mein ek conditional statement hota hai, jiska use switch statement ke sath kiya jata hai. Switch statement mein agar koi condition match nahi hoti hai toh default case execute ho jata hai. Iske alawa agar humne switch statement mein default case nahi diya hai toh wo kuch nahi karega aur simply switch statement se bahar aa jayega.

Question 74. What are break and continue words ?

Break aur Continue words Javascript mein use hotey hain loops ko control karne ke liye.

Break ka matlab hota hai loop ko khatam karna. Jab bhi tum break keyword ka istemal karte ho, loop us point tak execute hota hai jahan tak break ka istemal kiya gaya hai aur phir uska execution khatam hojata hai.

Agar tum continue keyword ka istemal karte ho, to loop mein se ek iteration skip hojata hai aur loop ki execution agle iteration se shuru hoti hai.

In summary, Break ka matlab hota hai loop ko puri tarah se khatam karna aur continue ka matlab hota hai loop ke ek iteration ko skip karke agle iteration se shuru karna.

Question 75. What is scoping ?

Scoping refers to the set of rules that determine where a variable is accessible or visible within a program. Scoping ek set of rules hai jo decide karta hai ki ek variable ko program ke kitne hisson se access kiya ja sakta hai aur kitne hisson se nahi.

Question 76. What are the different types of scopes in JavaScript? Explain each?

JavaScript mein alag-alag scopes hote hai: Global scope, Local scope or Function scope, Block scope.

Global scope un variables aur functions ko refer karta hai jo sabse upar level par define kiye gaye hai. Inhein program ke kisi bhi hisse se access kiya ja sakta hai.

Local ya Function scope ek function mein define kiye gaye variables aur functions ko refer karta hai. Ye variables aur functions sirf usi function mein accessible hote hai aur dusre functions ya program ke baki hisson se inka access nahi hota hai.

Block scope let aur const keywords ke use se block-level scope banaya jata hai. Ismein agar variable ko kisi block mein declare kiya gaya hai to woh block ke bahar access nahi ho sakta hai.

Question 77. What is function ?

JavaScript mein "function" ek aisa code block hai jiske andar kuch code likha hota hai jo baar-baar chalaya ja sakta hai. Ise ek baar define karne ke baad, isko multiple times call kiya ja sakta hai. Function define karne ke baad, usko arguments pass karke specific values ko calculate kar sakte hai aur result ko return kar sakte hai. Ye JavaScript mein reusability ke liye bahut important hota hai.

Question 78. What is functional programing ?

Functional programming in JavaScript is a programming paradigm that emphasizes writing code using pure functions that don't have any side effects and are designed to only operate on their input data. In simpler terms, it means writing code that performs a specific task without changing the state of the program or any other variables outside of the function. This makes the code more predictable and easier to reason about.

Question 79. What are generator function ?

Generator functions JavaScript mei ek special type ki function hai jisme yield keyword ka use kiya jaata hai. Ye function kuchh values generate karti hai, fir jab hum uss function ko call karte hain to wo values ko return karti hai. Lekin ye function poore values ko ek hi baar mei generate nahi karti balki ise hum multiple times call kar sakte hai aur har baar new value generate hogi.

Iska matlab ye hai ki generator function ek tarah se iterator bhi hai jo iterable objects ko iterate karta hai. Generator functions ko banana bohot simple hai aur ye bohot powerful tool hai async programming mei.

Question 80. What are the different ways of creating function in JavaScript?

JavaScript mein function banane ke kayi tarike hain. Kuch pramukh tarike hain:

  1. Function Declaration: Ismein hum function keyword ka istemal karte hain aur uske baad function ka naam likhte hain, jaise ki "function meraFunction() {}".

  2. Function Expression: Ismein hum ek variable ko function ke roop mein define karte hain, jaise "var meraFunction = function() {}".

  3. Arrow Functions: Ye function expression ki ek chunauti hai, ismein hum arrow operator (=>) ka upyog karte hain, jaise "const meraFunction = () => {}".

  4. Constructor Functions: Ismein hum constructor function ka upyog karte hain, jaise "function MeraClass() {}".

  5. Method Notation: Ismein hum object ke andar function ko define karte hain, jaise "{meraFunction: function() {}}".

  6. Class Syntax: Ismein hum ES6 ke class syntax ka upyog karte hain, jaise "class MeraClass {constructor() {} meraFunction() {}}".

Question 81. What is function declaration ?

JavaScript mein function declaration ek tareeka hai jisse hum ek naye function ko define karte hain. Ismein hum function ka naam likhte hain, uske baad parentheses mein arguments (agar koi hai) aur phir curly braces mein function ke code ko likhte hain.

Yeh function ko banane ke liye humari base hoti hai aur iske bina hum kisi bhi functionality ko perform nahi kar sakte hain. Function declaration ek aasaan aur zaroori concept hai JavaScript mein, kyunki iske use se hum apne code ko modularize kar sakte hain, jisse code ki readability aur maintainability badh jaati hai.

Question 82. What is currying in JavaScript?

Currying ek programming concept hai jismein function ko multiple arguments ke bajay ek argument at a time pass kiya jata hai. Jaise ki, agar hum ek function hai jiska naam add hai aur usmein do numbers add karna hai, toh usual tarike se hum add(2, 3) likhenge. Lekin currying mein hum add(2)(3) likhenge, yani pehle ek argument pass karenge phir dusra. Isse code ko modular aur reusable banaya ja sakta hai.

Question 83. What is function expression ?

Javascript mein function expression ek aisa tarika hai, jismein hum ek function ko variable mein store kar sakte hain. Ismein function ko ek naam nahi dena padta, balki hum usey seedhe variable ke through access kar sakte hain aur usey execute bhi kar sakte hain. Ye tarika bahut hi flexible hota hai, kyunki hum ek variable mein kisi bhi type ka data store kar sakte hain, jaise number, string ya phir function.

Question 84. What is arrow function ?

Arrow Function JavaScript mein ek shorthand syntax hai jo ki function ko likhne ke liye use kiya jaata hai. Ismein aap "=>" arrow symbol ka istemaal karte hain.

Iska upyog function ko define karne ke liye kiya jaata hai aur ye traditional function ke saamne kuch benefits provide karta hai, jaise ki shorter syntax, implicit return statement, and lexical scoping.

Udaharan ke taur par, traditional function:

function add(a, b) {
  return a + b;
}

Arrow function mein same kaam:

const add = (a, b) => a + b;

Question 85. What is anonymous function ? Where do we use this function?

JavaScript mein anonymous function ek aisa function hai jismein naam nahi hota. Yeh function usually doosre functions ke andar define kiya jaata hai ya phir direct call kiya jaata hai.

Iske liye hum "function" keyword ko use karte hain aur uske baad parentheses () lagate hain, jismein parameters define kiye jaate hain (agar koi parameter nahi hai toh bhi parentheses ka use kiya jaata hai) aur finally curly braces {} mein code likha jaata hai.

Hum isko variables mein store kar sakte hain ya phir event listeners, callbacks, timers, etc. mein use kar sakte hain.

For example:

// Anonymous function assigned to a variable
var add = function(a, b) {
  return a + b;
}

// Anonymous function as an event listener
document.getElementById("myButton").addEventListener("click", function() {
  alert("Button clicked");
});

Is tarah se hum JavaScript mein anonymous function ka use karte hain.

Question 86. What is the difference between function declaration and function expression ?

Function declaration aur function expression dono JavaScript mein functions ko define karne ke liye use kiye jaate hai. Function declaration mein, function ka naam direct likha jata hai, jabki function expression mein ek variable mein function ko assign kiya jata hai.

Jaise, function declaration:

function greet(name) {
  console.log("Hello " + name);
}

aur function expression:

var greet = function(name) {
  console.log("Hello " + name);
};

Function declaration ko code ke shuruaat mein define kiya jata hai, isliye iska scope puri file mein hota hai. Function expression ko variable mein store kiya jata hai, jisse uska scope variable ke scope ke andar hota hai.

Overall, dono ki basic functionality same hai, lekin unke define hone ka tareeka alag hai.

Question 87. What is the difference between function expression and arrow function ?

Function expression aur arrow function dono javascript mein ek tarah se functions hote hain par inme kuch differences hote hain.

Function expressions mein hum ek function ko variable ke andar define kar sakte hain, jise hum baad mein call kar sakte hain. Isme function keyword use hota hai aur apne arguments ko parentheses ke andar pass kiya jata hai. Example:

var myFunc = function(a, b) {
  return a + b;
}

Arrow function ka use bhi same hai par isme function keyword ki jagah => (arrow symbol) ka use kiya jata hai. Isme function ke arguments ko parentheses ke andar likha jata hai aur function body ko curly braces ke bina directly likha jata hai. Example:

var myFunc = (a, b) => a + b;

Iske alawa, arrow functions implicit return statement support karte hain. Matlab, agar function body mein sirf ek expression hai to use return keyword likhe bina hi return kara sakte hain. Example:

var myFunc = (a, b) => a + b;

Yehi same function expression ka ho toh woh return keyword ke saath likhna padega. Example:

var myFunc = function(a, b) {
  return a + b;
}

Question 88. What is difference between parameter and argument ?

Parameter aur argument dono Javascript mein function ke liye important hai. Parameter function ke declaration mein define kiye jaate hain, jabki argument function ko call karne ke waqt use kiye jaate hain.

Ek samanya udaharan se samajhte hain - Maan lo aapko ek function banana hai jo do integers ko add karega. Iske liye aapko function ko iss tarah declare karna hoga:

function addNumbers(num1, num2) {
  return num1 + num2;
}

Yahan par num1 aur num2 parameters hain, jo function definition mein define kiye gaye hain. Jab aap is function ko call karenge, tab aapko actual numbers provide karna hoga jiske saath operation perform karna hai, jaise:

addNumbers(5, 10);

Yahan par 5 aur 10 arguments hain jo function ko call karne ke waqt use kiye gaye hain.

Question 89. What are the default parameters? How to add default parameters in a function ?

Default parameters hote hai woh values jo function ke arguments ke liye set kiye jate hai jab woh arguments function call mein nahi diye jaate. Agar hum koi value pass nahi karte hai toh default parameter use ho jata hai.

Javascript mein default parameter add karne ke liye, hum function ke arguments ke baad '=' operator se default value assign kar sakte hai. Jaise ki:

function exampleFunction(arg1, arg2 = "default value") {
  // Function code here
}

Iss code mein, 'arg2' ka default value "default value" hai. Agar hum argument2 ko function call mein pass nahi karte hai toh yeh value use hogi.

Ummid hai ki samajh aaya hoga!

Question 90. What is call by value and what is call by reference ?

Call by value matlab jab hum kisi function mein argument pass karte hain toh uske ek copy kaam mein lete hain aur usko function ke ander use karte hain. Agar hum us argument ko function ke andar change karte hain toh wo change bahar ke variable ko affect nahi karta.

Call by reference matlab jab hum kisi function mein argument pass karte hain toh uska reference, yaani ki pointer, kaam mein liya jata hai aur agar hum us argument ko function mein modify karte hain toh wo change bahar ke variable ko bhi affect karta hai.

Question 91. What is High order function ? Give one example?

High order function ka matlab hota hai ki ek function dusre function ko as an argument leti hai ya fir ek function se dusra function return karti hai. Ek aisa example hai Array.prototype.map() function, jo ek array ke har element pe ek function apply karta hai aur ek naya array banata hai jismein har element ko transform kiya jaata hai.

Question 92. What is First class function ? Give one example?

JavaScript mein, first-class functions ka matlab hota hai ki functions ko first-class citizens ke roop mein treat kiya jaata hai, yani ki unhe variables mein store kiya ja sakta hai, dusre functions mein arguments ke roop mein pass kiya ja sakta hai, kisi function se return kiya ja sakta hai, aur object properties mein assign kiya ja sakta hai.

Example ke liye:

function add(x, y) {
  return x + y;
}

const myFunction = add; // function ko variable mein store karna
console.log(myFunction(2, 3)); // output: 5

function calculate(operation, x, y) {
  return operation(x, y);
}

console.log(calculate(add, 2, 3)); // output: 5

Question 93. What is callback function ? When to use callback functions?

Callback function ek tarika hai jisse hum ek function ko dusre function mein parameter ke roop mein pass kar sakte hain. Callback function ka upyog hum tab karte hain jab humein ek asynchronous kaam karna hota hai jismein humein pahle se nahi pata hota hai ki kitna samay lagega ya kya output hoga. Ismein hum callback function ka prayog karke, jab kaam pura ho jaata hai toh uss function ko call karte hain. Isse hum code ko efficient and responsive bana sakte hain.

Question 94. What is callback hell ?

Callback hell ek tarah ka code structure hai jismein bahut saare nested callbacks ek dusre ke andar hote hain. Isse code complex aur difficult to read ho jaata hai. Jab aap bahut saare asynchronous operations karte hain, jaise ki network requests ya file system access, tab callback hell hone ka zyada chance hota hai. Callback hell se bachne ke liye alternative techniques jaise Promises aur Async/await ka use kar sakte hain.

Question 95. What is setTimeout? How to clear a setTimeOut ?

setTimeout ek JavaScript function hai, jo code ko execute karne mein thoda time delay karta hai. Iska use aksar animations, user interactions, aur network requests ke liye kiya jata hai.

setTimeout function ek time interval aur ek callback function leta hai. Time interval aap milliseconds mein specify karte hain, aur callback function jo delay ke baad execute hoga woh bhi ismein pass karte hain.

setTimeout ka use karne ke baad agar aapko uss delay ko cancel karna hai, toh aap clearTimeout function ka use kar sakte hain. Ismein bas setTimeout se return kiya hua ID pass karna hota hai, jise clear karna hai.

Iske liye aapko yeh code likhna hoga:

const timeoutId = setTimeout(() => {
  // Code to be executed after delay
}, 1000);

// To clear the timeout:
clearTimeout(timeoutId);

Yahan timeoutId variable mein ID store ho raha hai, jo setTimeout ne return kiya tha. clearTimeout function call karke aap iss delay ko cancel kar sakte hain.

Question 96. What is setInterval? How to clear a setInterval ?

setInterval JavaScript mein ek function hai, jo humein diye gaye time interval ke baad ek code block ko baar baar execute karne ki anumati deta hai. Iske liye setInterval function mein do parameters hote hain, pahla function jisko hum baar baar run karana chahte hain aur dusra time interval kitna hona chahiye execution ke beech mein.

setInterval function se shuru ki gayi process ko band karne ke liye clearInterval function ka upyog kiya jaata hai. clearInterval function ko setInterval ke return value ke saath bhi use kiya jata hai. Ye setInterval ke execute hone wale process ko ruk deta hai aur usse rokta hai.

Simplified: setInterval ek function hai jo code ko regular intervals mein chalane ke liye use hota hai. clearInterval isi process ko stop karta hai.

Question 97. What are the call, apply and bind functions ? Give example of each.

  • call: Call method ka use karte hain jab hum kisi function ko call karna chahte hain aur uske liye ek specific this value aur arguments individually specify karna chahte hain.
  • apply: Apply method call method jaisa hi hai, lekin yahan parameter ke roop mein ek array pass kiya jata hai, jismein pehla parameter this value hota hai aur dusra parameter arguments ke form mein hota hai.
  • bind: Bind method ek naya function return karta hai jiske andar original function ke body hota hai lekin this value pehle se specified hoti hai. Isse hum naye function ko fixed this value ke saath create kar sakte hain aur optional arguments bhi specify kar sakte hain.

Example:

const person = {
  name: 'John Doe',
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

const anotherPerson = {
  name: 'Jane Doe'
};

person.greet.call(anotherPerson); // Output: Hello, my name is Jane Doe

Example:

const numbers = [1, 2, 3, 4, 5];

const sum = function() {
  return Array.prototype.reduce.apply(arguments, [(a, b) => a + b]);
}

console.log(sum(...numbers)); // Output: 15

Example:

const person = {
  name: 'John Doe',
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

const janeGreet = person.greet.bind({ name: 'Jane Doe' });
janeGreet(); // Output: Hello, my name is Jane Doe

Question 98. What is IIFE (Immediately invoked function expression) ?

IIFE (Immediately Invoked Function Expression) JavaScript mein ek tarika hai jisse hum ek function ko khud hi invoke kar sakte hain, matlab ki use call karne ki zaroorat nahin hai. IIFE ko hum apni coding mein variables aur functions ke scope ko manage karne ke liye istemal karte hain.

Ek IIFE aam taur par ek anonymous function hota hai, jo turant khud ko invoke kar leta hai. Iske liye hum function ko brackets mein daalte hain aur uske baad ek additional pair of brackets lagaate hain, jis se function ko khud hi call kar diya jaata hai.

Yah ek bahut hi upyogi technique hai jisse aap apne code ki security, readability aur maintainability improve kar sakte hain, kyonki IIFE mein define ki gayi variables aur functions sirf uske block mein hi accessible hote hain.

Question 99. What is closure ? When to use closure? What are the benefits of using closure?

Closure ka matlab hota hai ki ek function ke bahar define hua variable ya function us function ko access kar sakta hai, chahe wo function kahin aur bhi call ho.

Closure ka use karne ka fayda hai ki hum private variables aur functions create kar sakte hai jo publicly accessible nahi hote. Ye hume better control deta hai over our code.

Jab hum kisi module ya library banate hai toh hum closure ka use karte hai taki dusre developers hamare code ki implementation me directly interfere na kar sake.

Agar main simple words me bolu toh closure ek tarah se "kuch chizo ko pakad ke rakhna" hota hai jise future me hum use kar sakte hai.

Question 100. What is hoisting ?

Hoisting ek JavaScript phenomenon hai jahan pe variable aur function declarations ko "upar" move kiya jata hai uske scope ki shuruwat se pehle. Iska matlab hai ki aap ek variable ya function ko declare kar sakte ho baad mein bhi, lekin JavaScript interpreter use upar move karega uske current scope ke shuruwat tak.

Aisa lag sakta hai ki aapne kuch declare nahi kiya lekin wo actually hoist ho chuka hoga. Ye code ke behavior ko thoda tricky bana deta hai, isliye behtar hai ki aap apne code mei proper variable aur function declarations ka use karein.

Question 101. What is TDZ(Temporal Dead Zone) ?

TDZ (Temporal Dead Zone) ek aisi sthiti hai jahan variable ka istemaal karne se pehle use declare kiya gaya ho, lekin uske block scope ke andar nahi hai. Iske karan, variable ke liye memory allocate ho jaati hai, lekin access nahi kiya ja sakta hai kyunki wo TDZ mein hai. Isliye, agar hum TDZ mein declare kiye hue variable ko access karne ki koshish karte hain to "ReferenceError" exception throw hota hai.

Question 102. What is DRY Principle ?

DRY principle ka matlab hai "Don't Repeat Yourself". Ye ek programming concept hai jisme developers ko code ke duplication se bachna hota hai.

Iska matlab hai ki agar aap kisi functionality ko ek baar likh chuke hain, to aapko use bar-bar copy-paste karke repeat nahi karna chahiye. Aisa karne se aapki code readability aur maintainability kam ho jati hai.

Javascript mein DRY principle ka use functions, loops, classes, modules aur libraries ke through kiya jaata hai jisse code ko reusable banaya ja sakta hai. Yeh ek achhi practice hai jo code quality aur development speed dono ko improve karti hai.

Question 103. What is design pattern ? Name few famous design patterns?

Design Pattern JavaScript mein ek tarah ka reusable solution hota hai jo common programming problem ko solve karne ke liye use kiya jata hai. Ye ek accha tareeka hai code ko organize aur maintainable banane ka.

Kuch famous design patterns hai:

  1. Module Pattern: Ye pattern modules create karne ke liye use hota hai jis se ki code ko organized banake rakha ja sake.

  2. Singleton Pattern: Is pattern mein ek aisa object create kiya jata hai jo application mein ek hi baar instantiate hota hai.

  3. Factory Pattern: Factory pattern ek tarah ka creational pattern hai jis se ki objects ko create karne ke liye factories ka use kiya jata hai.

  4. Observer Pattern: Observer pattern ek tarah ka behavioral pattern hai jis se ki objects ke changes ko track kiya ja sake.

Design pattern ka use karke code ki quality improve ki ja sakti hai aur code ko scalable aur maintainable banaya ja sakta hai.

Question 104. What is developer tool?

Developer tool ek tarah ka software hota hai jo developer ko code likhne, debug karne aur application ya website banane ke liye madad karta hai. Ismein code editor, debugger, profiler aur version control system shaamil hote hain jo developer ko apne kaam ko sahi tarike se karne mein madad karte hain. Ye tools developers ke liye bahut mahatvapurn hote hain kyunki ye unko time bachate hain aur kam aasan bana dete hain.

Question 105. Difference between console.log and console.table ?

console.log aur console.table dono JavaScript mein debugging tools hain. console.log used hai single values or strings ko console mein print karne ke liye, jabki console.table used hai complex data structures jaise JSON objects or arrays ko organized tarike se display karne ke liye.

Toh sawal ye hai ki kya farak hai inn dono mein? Toh simple bhasha mein samjhaun toh console.log ek line mein data ko print karta hai, jabki console.table usko tabular form mein organize karke display karta hai. Isse data ko padhne mein aasani hoti hai specially jab humare paas bahut saara data ho.

Question 106. Difference between console.warn and console.error ?

Console.warn aur Console.error dono JavaScript mein debuggers hai jo kisi error ya warning message ko console mein display karte hain.

Lekin dono mein kuch antar hai. Console.warn sirf ek cautionary message deta hai, matlab ki code chalne ke waqt koi minor problem aayi hai lekin program abhi bhi sahi se chal raha hai. Iska matlab hai warn message ko ignore kiya ja sakta hai aur code chalne ke liye koi major issue nahi hai.

Wahi Console.error serious issues ko highlight karta hai jaise ki syntax errors, runtime errors ya koi critical logic error. Ye error message ko red text ke saath highlight karta hai aur aam taur par code ko ruk deta hai. Iska matlab hai ki error message ko fix kiya jana zaruri hai tabhi code sahi se kaam karega.

Toh in short, Console.warn minor problems ko highlight karta hai jabki Console.error critical errors ko show karta hai.

Question 107. What is Error? What are the different types of error in JavaScript?

Error ek aisi situation hai jab koi program ya code chalane mein kuchh gadbadi hoti hai aur iski wajah se expected output nahin mil paata hai. JavaScript mein kai prakaar ke errors hote hain, jaise:

  1. Syntax Errors: Jab aap kisi statement mein grammar ki mistake karte hain to syntax error aa jaata hai. Is error mein code execute hi nahin hota hai.

  2. Runtime Errors: Jab code ko execute karne ke dauraan koi error aa jaata hai to usko runtime error kaha jaata hai. Jaise ki undefined variable ya function call with wrong arguments.

  3. Logical Errors: Jab code to execute ho jaata hai par output galat aa jaata hai, to isko logical error kaha jaata hai. Yeh tab hota hai jab code mein koi logic ki kami hoti hai.

Question 108. Explain numbers in JavaScript?

JavaScript mein numbers ek tarah ke data type hote hai jo numerical values ko represent karte hai. JavaScript mein teen prakar ke numbers hote hai - integers, floating-point numbers aur special numbers jaise ki NaN aur Infinity.

Integers ke numbers purn sankhya hote hai jaise 1, 2, 3, -4, -5, aadi.

Floating-point numbers decimal numbers hote hai jaise 3.14, 2.5, 6.02E23 (scientific notation mein).

NaN yani Not-a-Number ek special value hota hai jo invalid numerical operations ke liye use hota hai.

Infinity ek aur special value hota hai jo numeric limit ke bahar lage values ko represent karta hai, jaise ki 1/0 ya Infinity.

Question 109. Difference between number and BigInt ?

JavaScript mein "number" aur "BigInt" ke beech ka antar yeh hai ki "number" variable size mein seemit rehta hai jabki "BigInt" variable unlimited size mein hota hai.

Jab hum koi bhi number type ki value "Number" variable mein store karte hain toh uski size 64-bit hoti hai. Iska matlab yeh hai ki humare paas sabse jyada 18 quintillion tak (10^18) ke numbers store karne ki capacity hoti hai. Agar hum isse bade numbers ko store karna chahte hain toh hume "BigInt" datatype ka upyog karna padta hai.

"BigInt" datatype mein aapko koi bhi limit nahi milegi kyunki yeh numbers ko multiple 64-bit chunks mein store karta hai. Lekin "BigInt" values par maths operations karne mein "Number" values ke comparison mein thoda slow ho sakta hai.

Question 110. What is Number System ?

Number system refers to the way numbers are represented and manipulated in JavaScript. It is basically the way we count and perform arithmetic operations on numbers in JavaScript.

JavaScript supports several number systems such as decimal (base 10), binary (base 2), octal (base 8), and hexadecimal (base 16). Decimal is the most commonly used number system in JavaScript, which uses 0-9 digits. Binary uses only two digits, 0 and 1, while octal uses 0-7 digits and hexadecimal uses 0-9 digits and A-F letters.

In JavaScript, you can convert between different number systems using functions like toString() and parseInt(). When performing arithmetic operations, JavaScript automatically converts numbers to the same base before performing the operation.

Question 111. Explain below functions of Number class toFixed,toString,valueOf,parseInt,parseFloat,isNaN

Jab hum JavaScript mein Number class ka upyog karte hain, tab kuch mahtvpurn functions hote hain jo niche diye gaye hain:

  1. toFixed() function: Yah function number ko di gayi desimal places ke anusaar round kar deta hai aur ek string ke roop mein return kar deta hai.

  2. toString() function: Yah function number ko string mein convert kar deta hai aur usko return kar deta hai.

  3. valueOf() function: Yah function number ki asli value ko return karta hai. Agar aap ek number variable ke sthaan par uska value chahte hain, to is function ka upyog kar sakte hain.

  4. parseInt() function: Yah function ek string ko integer mein convert kar deta hai. Iske liye, yah string mein se pahle se shuru hone wale numeric characters ko padta hai aur unhe integer mein convert kar deta hai.

  5. parseFloat() function: Is function ka upyog bhi string ko number mein convert karne ke liye kiya ja sakta hai, lekin agar ye floating point number hai to ye use correct tarike se parse karta hai.

  6. isNaN() function: Yah function check karta hai ki kya di gayi value "Not a Number" (NaN) hai. Agar di gayi value NaN hai, to iska matlab hai ki vah number nahin hai.

Question 112. What is Date object in JavaScript? What are the different ways of creating Date object?

Date object JavaScript mein ek built-in object hai jo dates aur times ko represent karta hai. Is object se hum current date aur time, ya phir specific dates aur times ko create kar sakte hain.

Date object banane ke liye kayi tarah ke tarike hote hain jaise ki:

  1. By default, jab hum koi new instance of Date object create karte hain bina parameters ke to vo current date aur time ko represent karta hai. Example: let currentDate = new Date();

  2. Hum specific date aur time ko bhi provide kar sakte hain. Date() constructor function ko use karke hum specific date aur time ko provide kar sakte hain. Example: let specificDate = new Date("April 4, 2023 10:30:00");

  3. Hum individual values bhi provide kar sakte hain year, month (0-based), day, hour, minute, second, milliseconds. Example: let specificDate = new Date(2023, 3, 4, 10, 30, 0);

In sab tarike se hum Date object ko create kar sakte hain aur isko apne kaam mein use kar sakte hain.

Question 113. How will you convert any Number or Date in a format of a difference locale/country ?

Agar aap ek number ya tareekh ko kisi aur desh/lok se milta-julta format mein badalna chahte hain to aap Javascript ki toLocaleString() method ka upyog kar sakte hain. Is method ke jariye aap kisi bhi desh/lok ke anusar date aur number ko alag-alag tarike se likh sakte hain.

Is method ka upyog karne ke liye, pehle us number ya date object par call karna hai. Iske baad aap is method ke andar desh/lok code aur options object pass kar sakte hain.

Desh/lok code ke liye, aap ISO 639-1 language codes aur ISO 3166-1 alpha-2 country codes ka istemaal kar sakte hain. Ye codes internet par aasani se dhoondhe ja sakte hain.

Iske baad options object mein aap tarike ko customize kar sakte hain, jaise ki decimal separator, grouping separator, currency symbol, time zone, etc.

Uparokt sabhi cheezon ko dhyaan mein rakh kar, aap Javascript mein kisi bhi number ya date ko kisi bhi desh/lok ke format mein badal sakte hain.

Question 114. What is DOM ?

DOM in javascript hain ek tree jaise structure jisme HTML elements ko represent kiya jaata hain. Iss tree ke nodes, HTML elements aur uske attributes hote hain. Javascript ka upyog karke hum DOM ko access aur manipulate kar sakte hain. Jaise ki kisi element ke text ko change karna, element ko hide karna ya naya element create karna.

Question 115. What is the need of DOM ?

Jab hum JavaScript ka use karte hain web development mein, tab humein website ke HTML structure ko manipulate karne ki zaroorat hoti hai - jaise ki koi element hide karna, content update karna, ya phir kuch naya add karna. Isko karne ke liye hum DOM (Document Object Model) ka istemal karte hain.

DOM basically ek tree-like structure hai, jisme har ek HTML element ko ek object ki tarah represent kiya jaata hai. JavaScript code dee gayi DOM ke through in objects ke saath interact kar sakta hai aur unhe modify bhi kar sakta hai. Iss tarah se, hum apni website ke HTML, CSS, aur JavaScript ko combine karke dynamic banane mein safalta paate hain.

Question 116. What are the below functions getElementById,getElementsByTagName, getElementsByClassName, querySelector, querySelectorAll, write ?

Ye sab functions HTML aur CSS ke code mei elements ya tags ko select karne ke liye use kiye jaate hai. Inka istemaal webpage ke design aur functionality ko improve karne ke liye kiya jata hai.

  1. getElementById - Is function se hum HTML document mei kisi specific element ko uske unique ID se select kar sakte hai.

  2. getElementsByTagName - Is function se hum HTML document mei kisi specific tag (jaise div, p, etc.) ke saare instances ko select kar sakte hai.

  3. getElementsByClassName - Is function se hum HTML document mei kisi specific class name se match karne wale saare elements ko select kar sakte hai.

  4. querySelector - Is function se hum CSS selector ka upyog karke kisi specific element ko select kar sakte hai.

  5. querySelectorAll - Is function se hum CSS selector ka upyog karke kisi specific group of elements ko select kar sakte hai.

Question 117. Difference between innerHTML vs textContent ?

innerHTML aur textContent dono Javascript mein element ke content ko access karne ke liye use kiya jaate hain. innerHTML HTML content ko bhi include karta hai, jabki textContent sirf plain text ko hi include karta hai. Agar aapke paas ek div element hai jisme kuch HTML tags hain, to innerHTML us HTML ko bhi show karega lekin textContent sirf uske content ka plain text hi dikhayega. Isliye agar aap HTML code ko modify karna chahte hain, toh innerHTML ka use karna sahi hai, lekin agar aap only text ko access karna chahte hain to textContent ka use karna sahi hai.

Question 118. What is an event bubbling ?

Event bubbling in JavaScript refers to the way events propagate or "bubble up" through nested elements in the HTML DOM (Document Object Model).

Let's say you have a button inside a div, and both of them have click event listeners attached. When you click on the button, the click event is first triggered on the button itself. Then, the event "bubbles up" to its parent element, the div, and triggers the click event listener attached to it as well.

This bubbling continues up the hierarchy of elements until it reaches the document object itself. Handlers for the same event type on outer elements will be called before handlers on inner elements.

So essentially, event bubbling means that an event that occurs on a nested element will also trigger any event listeners attached to its parent elements as it bubbles up through the DOM hierarchy.

Question 119. What is an event capturing ?

Event capturing ek tarika hai JavaScript mein jisse hum kisi event ka pata laga sakte hain ki wo DOM ke kis element se shuru hota hai. Jab hum kisi event ko capture karte hain to wo DOM tree ke top se bottom tak chalta hai, yani ki sabse upar wale element se start hota hai aur niche ki taraf jaata hai.

Isse hum DOM ke har ek element ko handle kar sakte hain aur apne code ko optimize karne mein madad milti hai.

Question 120. Difference between event bubbling and capturing ?

Event bubbling and capturing are two ways in which events propagate through the DOM (Document Object Model) in JavaScript.

Event bubbling is when an event starts at the deepest element and bubbles up through its parents until it reaches the document object. In other words, the event first triggers on the innermost element and then propagates outward to the outer elements.

On the other hand, event capturing is when an event is captured by the outermost element first and then propagates down to the innermost element. In other words, the event first triggers on the outermost element and then propagates inward to the inner elements.

So, the main difference between event bubbling and capturing is the order in which the elements receive the event. Bubbling goes from innermost to outermost, while capturing goes from outermost to innermost.

To remember this easily, you can think of event bubbling as a bubble rising to the surface of water, while event capturing is like a net capturing something from above.

Question 121. Difference between innherHTML and attribute ?

innerHTML aur attribute dono JavaScript ke tareeko hai webpage par HTML elements ko modify karne ke liye. innerHTML ko use karke hum HTML element ke andar ka content update kar sakte hai, jabki attribute se hum uss element ke properties jaise ki id, class, src, href aadi ko change kar sakte hai. Jaise ki naam se pata chalta hai, innerHTML mein hum HTML code daalte hai, jabki attribute mein hum sirf values daalte hai.

Agar humko ek div ke andar ka text update karna hai, to hum innerHTML ka use karenge, aur agar hum uss div ka id attribute change karna chahte hai, to hum attribute ka use karenge. Iske alawa, innerHTML ke use se hum new HTML elements bhi create kar sakte hai, jabki attribute se hum elements ko manipulate karte hai.

Question 122. How to change the style of an html element using DOM ?

HTML mein hum alag-alag prakar ke tags ka upyog karte hain, jaise <div>, <p>, <h1> aadi. In tags ke andar hum various properties aur attributes define karte hain, jaise ki class, id, style, src aadi.

Javascript mein DOM (Document Object Model) ka upyog karke hum HTML elements ke saare properties, attributes aur content ko access aur manipulate kar sakte hain. Agar hum kisi HTML element ke style ko change karna chahte hain, to hum us element ke style attribute ko access karenge aur usmein se desired property ko change karenge.

Jaise maan lijiye hume ek <div> element ki background color change karni hai, to hum us div element ke id ya class ko access karenge aur uska style.background property ko set kar denge. Ye kaafi simple hai:

// HTML mein div element definition
<div id="myDiv">My Div Element</div>

// Javascript mein style change karna
document.getElementById("myDiv").style.background = "blue";

Iss code snippet mein humne getElementById() function ka upyog karke HTML mein se myDiv ID wale element ko access kiya aur uska style.background property ko blue set kar diya. Is tarah hum kisi bhi HTML element ke style ko change kar sakte hain DOM ka upyog karke.

Question 123. What is event ?

JavaScript me "event" ek aisa khud-bakod (self-contained) chij hai jiske baad koi action hota hai, jaise user kisi button ko click karta hai ya koi element screen par dikhta hai. Is event ke saath-saath, ek function ya code bhi hota hai jo us event ko handle karta hai. Event-driven programming ka model is prakar se kaam karta hai ki jab tak koi event nahi hota, tab tak program rukha rehta hai aur event hone ke baad hi uska next step execute hota hai.

Question 124. What is role the event.preventDefult() ?

event.preventDefault() JavaScript mein ek function hai jo event ko rokta hai jisse default behaviour ho sakta hai, jaise ki ek form submit hone ke baad page reload karna. Agar aap event.preventDefault() ka use karte hain tab browser uss particular event ka default behaviour nahi karega.

Iska matlab hai ki jab bhi koi user form submit karta hai, aap apne JavaScript code mein event.preventDefault() ka use karke page ko reload nahi kar sakte hain aur aap apne form data ko back-end server pe bhej sakte hain XMLHttpRequest ya fetch API se.

Question 125. What are the different types of mouse/keyboard events ?

JavaScript mein mouse/keyboard events ke kuch alag-alag prakar hote hain.

  1. Click event - Jab aap mouse se kisi bhi element par click karte hain, to click event trigger ho jata hai.

  2. Double-click event - Jab aap mouse se kisi element ko double-click karte hain, to double-click event trigger hota hai.

  3. Hover event - Jab aap mouse ko kisi element ke upar le jate hain, to hover event trigger hota hai.

  4. Keydown event - Jab aap keyboard se koi key press karte hain, to keydown event trigger hota hai.

  5. Keyup event - Jab aap keyboard se koi key release karte hain, to keyup event trigger hota hai.

  6. Keypress event - Jab aap keyboard se koi key press karte hain, to keypress event trigger hota hai.

In events ko use karke aap apne JavaScript code ko user ke actions ke sath synchronize kar sakte hain aur apne web applications ko interactive aur responsive bana sakte hain.

Question 126. What is eventListener ?

Event listeners in JavaScript are functions that wait for a specific event to occur on a web page, such as a mouse click or a keyboard press. When the event happens, the function is triggered and performs a certain action. It's like a chowkidar who listens to specific sounds and alerts you when they occur.

Question 127. How to remove a eventListener ?

Event listener ko remove karne ke liye, removeEventListener() method ka upyog kiya jaata hai. Is method ko use karke event ko aur uske saath judi function ko hataaya ja sakta hai.

Jab aap kisi element par addEventListener() method se event listener add karte hai to uski return value ek reference hoti hai, jise aap later removeEventListener() method se use kar sakte hai.

Iska example niche diya gaya hai:

// Event listener add karna
myElement.addEventListener('click', myFunction);

// Event listener hataana
myElement.removeEventListener('click', myFunction);

Yahaan, myElement JavaScript element hai jispar event listener add karna hai. addEventListener() method ke dwara event listener add kiya jata hai, jismein 'click' event type aur myFunction function specify kiya gaya hai.

removeEventListener() method se ise hataya ja sakta hai, jismein bhi same event type aur function pass kiye jaate hai jo pahle add kiye the.

Question 128. How to create a DOM element dynamically ?

Javascript mein ek DOM element ko dynamic tarike se kaise banaya ja sakta hai?

Javascript ke madhyam se hum ek naya HTML element bana sakte hain, jisko Document Object Model (DOM) kahte hain. Iske liye hum createElement() method ka upyog karte hain jo ek naya element banane mein madad karta hai.

Jaise ki agar aap ek naya

element banana chahte hain, toh aapko yeh code likhna hoga:

var newDiv = document.createElement('div');

Iss code se ek naya div element banega jo ki abhi tak apne HTML page mein maujood nahi hai. Aap isse CSS ke madhyam se style kar sakte hain aur usme content add kar sakte hain, phir use apni website mein display kar sakte hain.

Question 129. What are the below BOM objects? Explain each Window, History, Navigator, Screen, Location, Timing,Cookies, LocalStorage

  1. Window - Window ek browser ka pramukh object hai jo ek web page ko darshata hai. Yeh browser ki current window ka reference deta hai aur usme sabhi dusre objects shamil hote hain.

  2. History - History object browser ki back and forward buttons ke saath juda hua hai. Isse aap apne browser ki history ko track kar sakte hain aur pichhle pages par laut sakte hain.

  3. Navigator - Navigator object browser ki information provide karta hai jaise ki kaunsa browser chala raha hai, uska version kya hai, operating system kaunsa hai, etc.

  4. Screen - Screen object browser ke current screen ki dimension aur resolution provide karta hai.

  5. Location - Location object browser ki current URL ko represent karta hai. Aap is object ka upyog karke URL ko read/write kar sakte hain.

  6. Timing - Timing object website ke performance ke liye bahut important hai. Isse aap apne webpage ke load time aur user response time ko measure kar sakte hain.

  7. Cookies - Cookies web server aur browser ke beech communication ko maintain karta hai. Ye small text files ke roop me store hote hain aur login session, shopping cart items, preferences, etc. ko save karna possible banate hain.

  8. LocalStorage - LocalStorage ek client-side storage mechanism hai jo web pages ke data ko client system pe store karta hai. Ye cookies se zyada storage provide karta hai aur long-term data storage ke liye upyogi hota hai.

Question 130. What is JavaScript engine? What are the famous JavaScript engine names?

JavaScript engine ek software program hota hai jo JavaScript code ko interpret karta hai aur machine-readable instructions me convert karta hai, jise computer samajh sakta hai. Ye web browsers, servers, mobile apps jaise places pe use kiya jaata hai.

Kuch famous JavaScript engines hai:

  • V8 (Google Chrome and Node.js)
  • SpiderMonkey (Mozilla Firefox)
  • JavaScriptCore (Safari)
  • Chakra (Microsoft Edge)
  • Nashorn (Java Virtual Machine)

In engines ke alag-alag tarike hote hai JavaScript ko interpret karne ka, jaise ki Just-in-Time (JIT) compilation, ahead-of-time (AOT) compilation, or interpretive execution.

Question 131. What is Call Stack ?

Call Stack JavaScript mein ek internal data structure hai jo functions ke calls ko track karta hai. Jab aap ek function call karte hai, to ye function Call Stack mein add ho jaata hai, aur jab wo function apna execution complete karta hai, to wo Call Stack se remove ho jaata hai. Jab bhi aap koi function call karte hai, to wo function ki jagah Call Stack par push ho jaati hai aur jab function ka execution khatam hota hai, to wo stack se pop ho jaati hai. Is tarah se, Call Stack hume batata hai ki kaunsa function abhi execute ho raha hai aur uske baad kaunsa function execute hoga.

Question 132. What is Execution Context ?

Execution Context ek tarah ka environment hota hai jisme JavaScript code execute hota hai. Har baar jab koi function call hota hai, toh uski execution context create hoti hai. Execution context mei sabse pehle Variable Object create hota hai, phir 'this' keyword initialize hota hai aur phir Function ka Execution bhi start hojata hai.

Question 133. How call stack and execution context are interrelated ?

Call stack aur execution context dono hi JavaScript ke runtime environment me important hai. Jab hum koi function call karte hai, toh woh function call stack me add hota hai aur uska respective execution context ban jata hai. Execution context me function ke sabhi local variables, parameters, aur 'this' keyword ka value store kiya jata hai. Jab function execute ho jata hai, toh uska execution context pop out ho jata hai call stack se.

Isi tarah se, jab nested functions calls hote hai, toh unki corresponding execution contexts bhi call stack par sahi sequence me add hote hai aur unke execution complete hone ke baad ek-ek karke call stack se nikalte hai. Yeh process chalta rehta hai jab tak ki call stack khali na ho jaye.

Question 134. What is Heap memory ?

Heap memory in JavaScript is where objects are stored when they are created during the program's execution. It is a large pool of memory that is dynamically allocated to the program and managed by the JavaScript engine. Think of it like a big container where all the variables, arrays, and objects live while your code is running.

Heap ka matlab hota hai bada khilauna. Heap memory ek aisi jagah hai jahan JavaScript ke program ke runtime main banaye gaye objects store hote hain. Ye memory block program ke liye dynamically allocate kiya jata hai aur JavaScript engine unhe manage karta hai. Isko aap ek badi container ke tarah imagine kar sakte hain jahan aapke variables, arrays aur objects aapke code execute ho rahe hai, wahin rehte hain.

Question 135. What is Stack memory ?

Stack memory in JavaScript is where the temporary data of a function is stored during its execution. Suppose you have a function that calls another function, then the calling function's stack frame gets pushed onto the stack and holds its local variables and parameters. When the called function completes its execution, the stack frame gets popped off the stack and control returns to the calling function's stack frame. This continues until the main function completes its execution and the stack becomes empty again. Essentially, stack memory in JavaScript is like stacking plates on top of each other - you can only access the topmost plate, and once you're done using it, you remove it and access the plate underneath.

Question 136. What is the difference between heap and stack memory ?

Heap aur stack memory dono hi JavaScript language ke andar hoti hai, lekin unki functionality alag hai.

Heap memory program ke dynamic allocations ke liye use hoti hai. Ye memory block, objects aur variables jaise large data structures ke liye reserve ki jaati hai. Jab aap koi object ya variable create karte hain to wo heap memory mein store hota hai. Isme allocate kiya gaya memory automatic garbage collector dwara release kiya jata hai jab uska use khatam ho jata hai.

Stack memory ka use function call stacks ke liye kiya jata hai. Ye memory block temporary variables aur function parameters jaise chhote data structures ke liye reserve kiya jata hai. Stack memory mein allocate kiya gaya memory automatically deallocate ho jata hai function call execution complete hone par.

Toh, Heap memory large data structures ke liye hoti hai jabki stack memory chhoti temporary data structures ke liye hoti hai jo function call execution ke saath deallocate ho jati hai.

Question 137. What is Compiler?

कॉम्पाइलर एक सॉफ्टवेयर होता है जो कोड को मशीन के लिए बिना त्रुटि के बदलता है। यह कंप्यूटर के प्रोसेसर के लिए समझने और निष्पादित करने में मदद करता है। इसके बिना, हम अपने कोड को सीधे कंप्यूटर मशीन में नहीं चला सकते हैं।

Question 138. What is interpreter?

Interpreter ek aisa program hai jo humare computer mein programming language se likhe gaye code ko samajhne aur execute karne ki madad karta hai. Interpreter line-by-line code ko padhkar usko turant execute karta hai aur agar koi error hota hai to use bata deta hai. Interpreter ka upyog jyadatar scripting languages jaise Python, Ruby, aur JavaScript mein hota hai. Interpreter mein code likhna asaan hota hai kyunki hum code likhte samay turant feedback milta hai.

Question 139. What is the difference between compiler and interpreter?

Compiler aur Interpreter dono programming languages ke code ko machine language me translate karne ka kam karte hai, lekin unke tarika alag-alag hai.

Compiler ek aisa program hai jo puri coding file ko ek sath read karke use machine code me convert kar deta hai. Ye machine code executable format me hota hai aur execute hone par humari application run karti hai. Compiler ki madad se hum apni code ko bahut jaldi aur efficient tarike se execute kar sakte hai.

Interpreter ek aisa program hai jo line by line code ko execute karta hai. Interpreter code ke errors ko jaldi detect karta hai kyunki wo use line by line read karta hai. Interpreter ki madad se hum code ko easily debug aur test kar sakte hai.

Toh, Compiler apne kaam ko poora karne ke liye puri file ka wait karta hai jabki Interpreter har line ko execute karta hai.

Question 140. What is JIT ?

JIT (Just-In-Time) JavaScript ek technology hai jo code performance ko improve karne ke liye use hoti hai. Jab hum JavaScript code ko run karte hai, toh isko pahle parse kiya jata hai aur phir execution hoti hai. Lekin, agar hum ek JIT compiler ka use karein, toh ye interpreter ko code ko compile karne ki permission deta hai, jisse execution time kam ho jaata hai aur code ki performance increase ho jaati hai. Isse JavaScript applications ki speed badh jaati hai.

Question 141. What is event loop ?

Event loop JavaScript mein ek aisa mechanism hai jisse code execution ko control karte hain. Ye tabhi kaam karta hai jab koi asynchronous function call hota hai, jaise ki setTimeout() ya XMLHttpRequest(). Is mechanism mein, sabhi events ek queue mein store hote hain aur event loop unhe ek ek karke execute karta hai. Jab ek event ke saare tasks complete ho jaate hain, tabhi dusra event execute hota hai. Ye cycle bar-bar chalta rahta hai jab tak saare events execute na ho jaayen.

Question 142. What is inside the execution context ? Types of execution context? , Global, Functional

Execution context ka matlab hota hai ki jab bhi hum JavaScript code ko run karte hain, toh uss time pe ek naya 'execution context' create hota hai. Yeh execution context banane ka kaam interpreter karta hai.

Execution context ke andar 3 chizon ka hona zaruri hota hai:

  1. Variable Object - yeh variable aur functions ke naam ko store karta hai
  2. Scope Chain - yeh execution context ke parent ko refer karta hai
  3. "this" keyword - yeh current object ko point karta hai

JavaScript mein do tarah ke execution context hote hain:

  1. Global Execution Context - yeh code ke bahar sabse pehle create hota hai aur program ke khatam tak rehta hai. Ismein 'this' keyword 'window' object ko point karta hai.
  2. Functional Execution Context - yeh function call hone pe create hota hai aur function ke khatam hone pe destroy ho jata hai. Ismein 'this' keyword function ke object ko point karta hai.

Iske alawa, JavaScript mein 'eval' function use karne se bhi execution context create hota hai par yeh deprecated method hai aur abhi istemal nahi kiya jaata.

Question 143. What is variable environment ?

Variable environment ek aisi jagah hai jahan pe JavaScript ke variables ka data store hota hai. Jab bhi hum koi variable create karte hai, JavaScript uska reference variable environment se leta hai aur use define karta hai. Variable environment mei variables ke alawa bhi functions aur objects ka data store hota hai. Ye variables, functions aur objects ke liye ek isolated space provide karta hai jo ki globally accessable nahi hota hai.

Question 144. What is scope chain ?

Scope chain in JavaScript refers to the hierarchy of variables and functions that a program can access within nested functions. Each function has its own scope, and variables declared within a function are only accessible within that function and its inner functions.

When a variable is referenced within a function, JavaScript first checks if it's defined within that function's scope. If not, it looks up the scope chain until it finds the variable or reaches the global scope. The order in which JavaScript looks up the scopes is called the scope chain.

In simple words, scope chain JavaScript mein ek tarika hai jisse program ke andarke function aur uske nested functions mein variables aur functions ko access kiya ja sakta hai. Har function apni alag scope rakhta hai aur ek function ke ander declare kiye gaye variables sirf usi function aur uske nested functions ke andar access kiye jaa sakte hai.

Jab koi variable ek function ke ander reference kiya jaata hai, toh JavaScript sabse pehle check karta hai ki woh variable usi function ke scope mein define hai ya nahi. Agar nahi hai toh woh scope chain ke through upar ki taraf check karta hai, jab tak woh variable mil nahi jaata ya phir global scope tak nahi pahunch jaata. Ye jo order hota hai jisse JavaScript scopres ko search karta hai, use hum scope chain kehte hai.

Question 145. What is this keyword ? Where to use it? What this keyword denotes?

"this" keyword ka matlab hai "yeh". Javascript mein "this" keyword use hota hai object ke reference ko refer karne ke liye, jaise ki current object, function ya method.

Jab aap kisi object ke andar function define karte ho, toh uss function mein "this" keyword uss object ko refer karta hai. Isko hum "method" kehte hai.

Agar aap "this" keyword bina kisi object ke use karte ho, toh yeh global object ko refer karega.

Isliye, "this" keyword ka sahi use karke aap apne code ko clean aur maintainable bana sakte ho.

Question 146. What is creation and code phase ?

"Creation phase" aur "Code phase" dono hi JavaScript ke execution ka hissa hai. Creation phase me, JavaScript engine ne variables aur functions ko memory me allocate kar diya jata hai. Jabki code phase me, ye allocated variables aur functions ko use karke program execute karta hai.

Iska matlab hai ki jab aap apna JavaScript code run karte ho to sabse pehle engine apne dimaag me sochta hai ki "Konsa variable ya function banana hai?" aur fir usko memory me allocate kar deta hai. Is process ko creation phase kehte hai. Uske baad, engine code ko line by line chalta hai aur usko execute karta hai. Ye process code phase kehte hai.

In summary, creation phase me variables aur functions ko allocate kiya jata hai, jabki code phase me unka use kiya jata hai.

Question 147. What will happen if we use this keyword inside a function in strict mode ?

Agar hum ek function mein 'this' keyword strict mode mein use karenge toh yeh us function ke execution context se related hoga, aur global object ke saath bind nahi hoga. Iska matlab hai ki agar 'this' ko undefined ya null assign nahi kiya gaya hai, toh function call error throw karega.

Question 148. What is the difference between primitive and object ?

JavaScript me primitive aur object dono hi data types hote hai. Primitive, ek simple data type hota hai jaise ki number, string, boolean, null aur undefined. Jabki object, ek complex data type hota hai jisme bahut saare properties aur methods hote hai.

Primitive values ko direct assign kiya ja sakta hai variable me, jabki object ko create karne ke liye new keyword ka use karna hota hai. Primitive values ko immutable mana jata hai matlab ki unhe modify nahi kiya ja sakta hai, jabki object ki values aur properties change ki ja sakti hai.

Primitive types value ke comparison ke liye == operator ka use kiya jata hai, jabki object reference ke comparison ke liye == operator ka use kiya jata hai. Agar dono operands same object ko point karte hai to true return hota hai, warna false return hota hai.

Iske alawa, primitive values stack me store hote hai aur object heap memory me store hota hai.

Question 149. What is normal copy ?

Normal copy Javascript mein ek tarika hota hai jisse hum kisi variable se dusre variable mein data copy kar sakte hain. Iske liye assignment operator (=), Object.assign() method ya spread operator (...) ka use kiya ja sakta hai.

Question 150. What is shallow copy ? How to perform shallow copy of an object?

Shallow copy refers to creating a new object that has the same properties as the original object, but shares the same references to values stored in memory. This means that changes made to the values of the shallow copied object will also affect the original object.

To perform a shallow copy of an object in Javascript, you can use various methods such as Object.assign() or the spread operator (...). For example:

const originalObject = {name: "John", age: 25};
const copiedObject = Object.assign({}, originalObject); // shallow copy using Object.assign()
const anotherCopiedObject = {...originalObject}; // shallow copy using spread operator

console.log(copiedObject); // {name: "John", age: 25}
console.log(anotherCopiedObject); // {name: "John", age: 25}

copiedObject.age = 30;
console.log(originalObject); // {name: "John", age: 25}
console.log(copiedObject); // {name: "John", age: 30}
console.log(anotherCopiedObject); // {name: "John", age: 25}

Isme hum ek naya object banaate hain jo original object ki properties ko copy kar leta hai. Lekin yeh reference values ko same memory me point karta hai, isliye agar hum copied object ke values ko update karte hain toh original object ke values bhi update ho jaate hain.

Question 151. What is deep copy ? How to perform deep copy of an object?

Deep copy refers to creating a separate and independent copy of an object, with its own set of properties and values. This means that any changes made to the copied object will not affect the original object.

To perform deep copy of an object, you can think of it like creating a photocopy of a document. Just as a photocopy is an exact replica of the original document, a deep copy of an object is an exact replica of the original object.

In code, you can perform a deep copy of an object using methods such as JSON.parse(JSON.stringify(object)), which converts the object to a JSON string and then parses it back into a new object. Another way is to use libraries like lodash or jQuery that have built-in functions for deep copying objects.

For example, if you have an object named originalObject, you can create a deep copy of it like this:

var copiedObject = JSON.parse(JSON.stringify(originalObject));

This will create a new, separate object called copiedObject that has the same properties and values as originalObject.

Question 152. Where objects are stored ? Is object primitive type or non-primitive?

JavaScript mein, objects memory mein heap par store hote hain. Objects non-primitive type hote hain, matlab ki wo variables mein seedhe store nahi hote, balki unki memory location ka reference variable mein store hota hai.

Question 153. What is Destructuring ?

Destructuring ek tarika hai jisme hum ek object ya array ke andar ke values ko directly variables mein assign kar sakte hai. Jaise ki, agar humara ek object hai:

const person = {
  name: 'John',
  age: 30,
  city: 'Mumbai'
}

Toh destructuring ka use karke hum in values ko variables mein assign kar sakte hai:

const { name, age, city } = person
console.log(name) // Output: John
console.log(age) // Output: 30
console.log(city) // Output: Mumbai

Yeh code person object ke andar ke values ko name, age, aur city variables mein assign karta hai. Isi tarah se hum arrays ke andar ke values ko bhi variables mein assign kar sakte hai.

Question 154. How to reverse values using destructuring ?

Destructuring ka istemaal karke Javascript mein values ko ulta kaise karen?

Agar hum ek array ya object se values ko reverse karna chahte hain, toh destructuring ka istemaal kar sakte hain. Array ke liye, hum [...array].reverse() likhenge. Object ke liye, hum {...object} ke jagah {...object}.reverse() nahi likh sakte kyunki objects key-value pairs se bane hote hain aur keys ko reverse nahi kiya ja sakta hai.

Question 155. How to return two values from function ?

JavaScript mein hum ek object ya array mein multiple values ko combine karke ek hi value return kar sakte hain. Iske liye, hum ek object mein key-value pairs ka use kar sakte hain ya phir ek array mein values ko sequence mein store kar sakte hain. Fir hum ye object ya array ko function ke andar create kar sakte hain aur function se return kar sakte hain. Function se return kiya gaya object ya array ko fir se destructure karke hum multiple values ko alag-alag variables mein assign kar sakte hain.

Jaise example ke taur par:

function getValues() {
  let name = "John";
  let age = 30;
  return {name, age}; // Object with key-value pairs
}

let {name, age} = getValues(); // Destructuring object to get individual values

console.log(name); // Output: John
console.log(age);  // Output: 30

Ya phir,

function getValues() {
  let name = "John";
  let age = 30;
  return [name, age]; // Array with values in sequence
}

let [name, age] = getValues(); // Destructuring array to get individual values

console.log(name); // Output: John
console.log(age);  // Output: 30

Question 156. How destructuring works in array and object ?

Destructuring ka matlab hota hai kisi bade data structure se chote chote hisse nikalna. Iska use hum JavaScript mein arrays aur objects se values extract karne ke liye karte hain.

Agar hum array destructuring ki baat karein toh, hum ek array se uske elements ko variable mein store kar sakte hain. Ye hamare code ko short aur readable bhi banata hai. Jaise:

const myArray = [1, 2, 3];
const [a, b, c] = myArray;
console.log(a); // Output: 1
console.log(b); // Output: 2
console.log(c); // Output: 3

Agar hum object destructuring ki baat karein toh, hum ek object se uske key-value pairs ko variables mein store kar sakte hain. Jaise:

const myObject = {name: "John", age: 30};
const {name, age} = myObject;
console.log(name); // Output: John
console.log(age); // Output: 30

Is tarah se hum destructuring ka use karke arrays aur objects ke elements ya key-value pairs ko variables mein store kar sakte hain.

Question 157. How to do destructuring of nested array ?

Nested arrays refer karte hai jab ek array ke andar dusre arrays hote hai. Jab hum nested arrays ko access karne ki baat karte hai, tab destructuring ka use karte hai.

Destructuring se hum variables ko maintain karke array ke specific values ko extract kar sakte hai. Agar hum nested array ke liye destructuring karna chahte hai toh hum ek bracket aur comma ka use karenge jiske through hum uss array ke andar dusre array ke elements tak pahunch sakte hai.

For example:

const myArray = [1, 2, [3, 4]];

const [a, b, [c, d]] = myArray;

console.log(a); // Output: 1
console.log(b); // Output: 2
console.log(c); // Output: 3
console.log(d); // Output: 4

Upar diye gaye code mein, myArray ek nested array hai jo const keyword se define kiya gaya hai. Fir hum myArray ko a, b, c aur d variable mein destructuring kar rahe hai. a variable mein myArray ke pehle element 1 store ho raha hai, b variable mein dusra element 2 store ho raha hai aur c aur d variables mein third nested array ke dono elements 3 aur 4 store ho rahe hai.

Isi tarah se aap bhi JavaScript mein destructuring ka use karke nested arrays ke Elements ko access kar sakte hai.

Question 158. How to set default values in destructuring ?

Javascript mein destructuring mein default values kaise set karein?

Destructuring mein agar hum variable ko extract karte hain toh usmein undefined value aati hai, lekin agar humne uski jagah koi default value set kar diya toh wo use hogi. Iske liye hum variable ke saath colon (:) laga kar uska default value set kar sakte hain.

For example:

const { name = 'John', age = 30 } = person;

Yahaan, person object se name variable ko extract karte waqt agar name property na ho toh uski jagah 'John' value set ho jayegi aur agar age property na ho toh uski jagah 30 value set ho jayegi.

Question 159. What is destructuring object ?

Destructuring object ek tareeka hai jisme hum JavaScript mein ek object ke values ko variables mein alag-alag tarah se store kar sakte hain. Ismein hum object ke properties ke naam ka upyog kar ke variables ko declare karte hain aur phir us object ke values ko variables mein assign karte hain. Yeh code likhne aur padhne dono ke liye aasaan hota hai aur isse code ki readability bhi badhti hai.

Question 160. How we can use destructuring with object ?

Destructuring ek aisa technique hai jisme hum JavaScript mein object se values ko nikal sakte hain. Hum ismein curly braces {} ka use karte hain jisse specific properties ko access kiya ja sakta hai.

Hum destructuring ko variables ke through bhi kar sakte hain jisme hum direct object se property ko variable mein assign kar dete hain.

Example:

// Object destructuring
const person = { name: 'Raj', age: 25, city: 'Mumbai' };
const { name, age } = person;
console.log(name); // Output: Raj
console.log(age); // Output: 25

// Variable destructuring
const {city} = person; // Assigning the value of person.city to the variable city
console.log(city); // Output: Mumbai

Iss tareeke se hum JavaScript mein objects se values ko extract aur access kar sakte hain.

Question 161. How to destructuring remaining value to a single variable ?

JavaScript mein destructuring ka upyog karke hum ek se zyada variables se data ko alag alag variables mein nikal sakte hai. Agar humein kisi array ya object se kuch values nikalni hai aur baaki ke sare values ko ek variable mein store karna hai, to hum "rest" operator ka upyog kar sakte hai.

Rest operator ke liye hum "...variableName" ka use karte hai jahaan "variableName" ek variable ka naam hota hai jismein hum baaki ke sare values store karna chahte hai. Is tarah se, hum ek se zyada variables se data ko extract kar sakte hai aur baaki ke sare values ko single variable mein store kar sakte hai.

For example:

const [first, second, ...remaining] = [1, 2, 3, 4, 5];
console.log(remaining); // Output: [3, 4, 5]

Iss example mein, humne ek array se "first" aur "second" variables mein pehle do values nikali aur baaki ke sare values ko "remaining" variable mein store kiya.

Question 162. How to give alias name to a property name ?

JavaScript mein kisi bhi property ka alias naam dene ke liye, hum uss property ko ek naya variable mein store kar sakte hai jis ka naam hum chahate hai. Isse hum uss property ko dusre naam se access kar sakte hai.

For example: Agar aapko 'fullName' property ke liye 'pooraNaam' naam ka alias dena hai toh aap ye code use kar sakte hai:

// Initialize object
var person = {
  fullName: "John Doe"
};

// Create alias for fullName property
person.pooraNaam = person.fullName;

// Access property using alias name
console.log(person.pooraNaam); // Output: John Doe

Iss tarike se aap JavaScript mein kisi bhi property ka alias naam de sakte hai.

Question 163. How we can set default values in destructurnig object ?

Jab hum JavaScript mein objects ko destructuring karte hain, tab hum default values bhi set kar sakte hain. Iske liye, hum object ke property ke saath ek equal sign laga kar default value de sakte hain. Agar object mein woh property nahi hai toh default value use hogi. Yeh code example explain karta hai:

// Example object
const person = {
  name: 'John',
  age: 30,
};

// Destructuring with default values
const { name, age, gender = 'unknown' } = person;

console.log(name);   // Output: 'John'
console.log(age);    // Output: 30
console.log(gender); // Output: 'unknown', kyunki 'gender' property 'person' object mein nahi thi.

Is example mein, gender property person object mein nahi tha lekin jab hum destructuring kiya, default value 'unknown' set ho gaya.

Question 164. What is nested object destructuring ?

Javascript mein nested object destructuring ka matlab hai ki hum ek object ke andar dusre objects ko destructuring kar sakte hain. Isse hum multiple variables ko ek saath extract kar sakte hain aur unhe alag-alag variables mein store kar sakte hain.

Jaise ki, agar humare paas ek aisa object hai jismein doosre objects hain toh hum usmein nested object destructuring ka use kar sakte hain. Iske liye hum {} ke andar brackets laga kar variables ko define karte hain aur phir uss object ko access karte hain. Example ke taur par, agar humare paas ek aisa object hai:

const student = {
  name: "Rahul",
  age: 20,
  address: {
    city: "Mumbai",
    state: "Maharashtra"
  }
};

Toh usmein nested object destructuring ka use karke hum address object ke properties ko alag variables mein extract kar sakte hain, jaise ki:

const { name, age, address: { city, state } } = student;

console.log(name); // Output: Rahul
console.log(age); // Output: 20
console.log(city); // Output: Mumbai
console.log(state); // Output: Maharashtra

Is tarah se hum nested object destructuring ka use karke code ko readable aur efficient bana sakte hain.

Question 165. How to destructruing object when passing into a function ?

Jab aap ek object ko kisi function mein pass karte hai, toh aap us object ke alag-alag properties ko bhi alag-alag variables mein store kar sakte hai. Isko hum "destructuring" kehte hai.

Iske liye aapko function ke arguments mein object ka naam dena hoga, aur phir uske andar curly braces laga kar property names ko mention karna hoga jinhe aap alag-alag variables mein store karna chahte hai.

Example:

function myFunction({name, age}) {
  console.log(name);
  console.log(age);
}

const person = { name: 'John', age: 30 };

myFunction(person); // Output: John\n30

Yahan myFunction mein person object ko argument ke roop mein pass kiya gaya hai aur argument mein {name, age} diya gaya hai. Isse name aur age variables mein person ke corresponding values store ho jayenge.

Question 166. What is spread operator ?

Spread operator ek tarika hai jisme hum ek array, object ya iterable ke elements ko alag alag variables mein alag kar sakte hain. Yeh "..." (teen dots) se represent kiya jaata hai.

Iska upyog hum karte hain jab humein kisi bhi iterable ke saare elements ko individually access karna ho, unmein kuch changes karna ho ya unhein kisi function mein arguments ke roop mein pass karna ho. Spread operator ka istemaal aapke code ko kam aur efficient bana sakta hai.

For example:

// Array ke elements ko alag alag variables mein alag karna
const arr = [1, 2, 3];
const [a, b, c] = arr;
console.log(a); // output: 1
console.log(b); // output: 2
console.log(c); // output: 3

// Objects ke properties ko copy karna
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1, c: 3 };
console.log(obj2); // output: { a: 1, b: 2, c: 3 }

// Function mein arguments ke roop mein spread operator ka upyog karna
function sum(a, b, c) {
  return a + b + c;
}
const nums = [1, 2, 3];
console.log(sum(...nums)); // output: 6

Question 167. How to do shallow copy using spread operator ?

Shallow copy ka matlab hota hai ki ek object ke references ko dusre object me copy karna. Spread operator ka use karke hum shallow copy kar sakte hai.

Spread operator ... ka use karke hum array ya object ke values ko alag alag variables me spread kar sakte hai, jisse unhe easily manipulate kiya ja sake.

Iss tarah se hum spread operator ka use karke shallow copy kar sakte hai:

const originalObject = { name: "John", age: 30 };
const copiedObject = { ...originalObject };

Yahan humne ... ka use karke originalObject ke values ko copiedObject me copy kiya hai. Ab dono objects alag hai lekin unke values same hai.

Iss tarah se hum spread operator ka use karke shallow copy kar sakte hai.

Question 168. How to join multiple arrays using spread operator ?

Agar aap multiple arrays ko join karne chahte hain to aap spread operator ka upyog kar sakte hain. Iske liye aap simply ... operator ko use kijiye aur phir sabhi arrays ko ek sath pass kijiye. Jaise ki niche diya gaya hai:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const array3 = [...array1, ...array2];

console.log(array3); // Output: [1, 2, 3, 4, 5, 6]

Ismein, humne pehle do arrays array1 aur array2 ko declare kiya aur phir unhein ... operator se combine kar diya. Phir humne in dono arrays ko array3 mein store kiya. Ab array3 mein combined elements hain jinmein pehle array1 ke elements hain aur phir array2 ke elements hain.

Question 169. How to convert a String to array using spread operator ?

Javascript mein, ek string ko array mein tabdeel karne ke liye, hum spread operator ka upyog kar sakte hain. Spread operator (...) ka istemaal array elements ko unfold karne ke liye kiya jaata hai.

Iss prakriya mein, hum aise karte hain:

  1. Sabse pehle, hum apni string ko ek variable mein store karte hain.

  2. Fir hum uss variable ke saath "split" function ka upyog karte hain jisse vah string ko array mein convert karta hai.

  3. Ab hum spread operator ka upyog karte hain, "[...]" likh kar, jo iss array ko unfold karke uske elements ko le leta hai aur unhein naye ek array mein daal deta hai.

Iss tareeke se, hum ek string ko array mein badal sakte hain:

// String to Array using Spread Operator
let str = "Hello";
let arr = [...str];
console.log(arr); // Output: ["H", "e", "l", "l", "o"]

Yahan, hum ne string ko "str" variable mein store kiya aur phir usko array mein convert kar diya "split" function ka upyog kar ke. Fir hum spread operator ka upyog karke uss array ko unfold karke uske elements ko alag alag lai kar naye array mein daal diya. Output mein, hum dekh sakte hain ki string "Hello" ab array ke roop mein mojood hai.

Question 170. How to pass arguments in function using spread operator ?

Spread operator (yaani "...") ka use karke hum ek array ya object ke values ko expand kar sakte hain aur phir unhe alag-alag arguments ki tarah function mein pass kar sakte hain. Jaise ki:

function add(a, b, c) {
  return a + b + c;
}

const numbers = [1, 2, 3];

console.log(add(...numbers)); // Output: 6

Is example mein, humne add function ko define kiya jismein teen parameters a, b, aur c hain. Phir humne numbers variable mein ek array [1, 2, 3] assign kiya. Spread operator (...) ka use karke humne numbers array ke values ko add function ke arguments ki tarah pass kiya. Jaise hi add(...numbers) line execute hui, JavaScript ne 1, 2, aur 3 values ko add function ke corresponding parameters mein assign kiya aur a + b + c ka result 6 output kiya.

Isi tarah se hum multiple arrays ya objects ke values ko bhi expand karke function mein pass kar sakte hain aur uss function mein phir se array ya object banakar unhe return kar sakte hain.

Question 171. What is rest parameter ?

Rest parameter JavaScript mein ek feature hai jahaan aap function ke arguments ko flexible tareeke se handle kar sakte hain. Rest parameter ka use karke, aap multiple arguments ko ek array ke andar collect kar sakte hain.

Meaning, agar humein function mein kuchh bhi arguments pass karna hai aur unhein all together ek variable mein store karna hai, toh hum rest parameter ka use karte hain. Isse aapko zyada flexibility mil jaati hai kyunki ab aap jitne chahe arguments pass kar sakte hain aur unhein ek hi jagah par handle kar sakte hain.

Question 172. Difference between spread and rest operator ?

Spread aur Rest operator dono JavaScript ke important operators hai, par dono ke kaam alag-alag hai.

Spread operator (...) ek array, object ya string ko unpack karke uske saare elements ko alag-alag variables mein rakh deta hai. Iska use hum karte hai jab hum kisi function mein multiple arguments pass karna chahte hai.

Rest operator (...variableName) bhi spread operator ki tarah hi dots (...) se shuru hota hai, par iska use hum karte hai jab hum function mein variable number of arguments pass karna chahte hai. Rest operator se hum argument ko ek array ke form mein collect kar sakte hai.

Is tarah se, spread aur rest operator dono useful hote hai, par unke kaam alag-alag hai.

Question 173. How to pass variable arguments in function using rest operator ?

Rest operator JavaScript mein ek special syntax hai jo humein function mein variable arguments ko pass karne ki suvidha deta hai. Isse hum function ke andar multiple arguments ko single array mein collect kar sakte hain.

Iska use karne ke liye, hum function ke last parameter ke pehle "...(three dots)" ka use karte hain. Jaise ki:

function exampleFunction(...args) {
  console.log(args);
}

Isme ...args ek array hai jiske andar saare arguments collect ho jaayenge. Agar hum is function ko exampleFunction(1,2,3) call karenge toh console mein [1,2,3] print hoga.

Yeh tarika bahut flexible hai kyunki aap jitne chahe arguments pass kar sakte hain aur unko collect kar ke kuch bhi operation perform kar sakte hain.

Question 174. What is short circuiting ?

Short circuiting in JavaScript refers to a behavior where the evaluation of an expression stops as soon as the result can be determined without evaluating the entire expression.

Imagine you are trying to find your missing phone in your house. You start searching from one room to another, but as soon as you find it in the living room, you stop searching in the remaining rooms because there is no point in continuing the search anymore. This is similar to how short circuiting works in JavaScript - if the result can be determined early on, further evaluation is stopped to save time and resources.

Question 175. Difference between || and ??

|| and ?? are dono JavaScript mein use hone waale operators hai.

|| operator ek "logical OR" operator hai jo do operands ke beech mein istemaal hota hai. Jab koi bhi operand "true" hoga, toh yeh operator "true" value ko return karega. Agar dono operands "false" hote hai, toh last operand ki value return hogi.

?? operator ek "nullish coalescing" operator hai jo bhi do operands ke beech mein istemaal hota hai. Yeh operator checks karta hai ki pehla operand null ya undefined toh nahi hai. Agar pehla operand null ya undefined hai, toh yeh second operand ki value return karega, warna first operand ki value return hogi.

Toh, in short, || ka istemaal karte hai jab humein kisi ek condition true honi chahiye, aur ?? ka istemaal tab karte hai jab humein kisi value ki existence check karni ho aur null ya undefined nahi hona chahiye.

Question 176. What is nullish coalescing operator ?

Nullish coalescing operator ?? is a way to check for the existence of a value and return it if it's not null or undefined, otherwise return a default value. It is similar to the logical OR (||) operator, but with a key difference - the ?? operator only returns the default value if the original value is null or undefined, whereas the || operator will also return the default value if the original value is falsy (e.g. empty string, 0, NaN).

For example, consider the code const result = a ?? b. If a is not null or undefined, result will be a. However, if a is null or undefined, result will be b. This can be useful when you want to provide a default value for a variable or parameter, without accidentally using a falsy value as the default.

Question 177. What is optional chaining? What are the benefits of using optional chaining ?

Optional chaining ek JavaScript feature hai jisme hum object ke properties ya method pe access karne se pehle check kar sakte hai ki wo null ya undefined to nahi hai. Isse code likhne me errors kam hote hai aur code ko strong banane me help milti hai.

Iska matlab hai ki jab hum kisi object ki property ya method pe access karne ka try karte hai, agar wo object khud hi null ya undefined ho to code me error aata hai. Lekin optional chaining ka use karke hum is error ko avoid kar sakte hai.

Iss feature se humara code robust hota hai kyunki hum errors ko handle karke apna code run karate hai aur iss se humari productivity increase hoti hai kyunki hum errors ko handle karne ke liye extra time spend nahi karte hai.

Question 178. What is an Array ?

Array ek JavaScript me data ko store karne ka ek tarika hai jisme multiple values ko ek variable ke andar rakha ja sakta hai. Isko hum ek box samajh sakte hai jisme hum multiple cheezein daal sakte hai aur unko later mein use bhi kar sakte hai. Har value ko ek unique index number se access kiya ja sakta hai, jisme sabse pehle index 0 hota hai. Ye ek bahut hi powerful concept hai jo programming mein kaafi use hota hai.

Question 179. What are the properties of an Array ?

Arrays are a type of container in JavaScript which can hold multiple values of different types, such as numbers, strings, objects, and even other arrays. Here are some important properties of arrays in JavaScript:

  1. Indexing: Each element in an array is assigned a unique index number, starting from zero.

  2. Length: The length property is used to get the number of elements in an array.

  3. Mutability: Arrays in JavaScript are mutable, meaning that their contents can be changed by adding or removing elements.

  4. Iteration: Arrays can be easily iterated over using loops or built-in methods like forEach().

  5. Methods: There are several built-in methods available for arrays, such as push(), pop(), shift(), unshift(), splice(), slice(), etc., that can be used to manipulate them easily.

In short, arrays are an essential data structure in JavaScript that can store multiple values and offer various methods to manipulate them.

Question 180. Difference between Array Literal and Array Object ?

Array Literal aur Array Object dono ek saath khush hai, par unmein kuch antar hai.

Array Literal ek simple tareeka hai array ko banane ka, jisme hum [] (square brackets) ka prayog karte hain. Ismein humein items ko comma separated daalna hota hai. Jaise:

var fruits = ["apple", "banana", "orange"];

Wahi Array Object ek constructor function hai jo ek naya array banata hai. Ismein hum new keyword ka prayog karte hain. Jaise:

var fruits = new Array("apple", "banana", "orange");

Ek important baat jo dhyaan mein rakhni chahiye ki Array Object mein kabhi bhi sirf ek hi argument pass karna chahiye kyunki iska matlab ho sakta hai ki aapko uss size ki array chahiye jo 0 se shuru hoti hai. Jaise:

var arr = new Array(5); // Creates an empty array with length 5

Isliye, agar humein ek array of items banane hain toh Array Literal ka prayog karna zyada behtar hota hai.

Question 181. What is the significance of index in Array ?

Array mein index ka matlab hota hai ki har item(jo array ke element hote hai) ko ek unique number se identify kiya ja sakta hai.

Jaise ki hum log bookshelf se koi bhi kitab nikalne ke liye uski position yaad rakhte hai, thik waise hi hum array ke saare elements ko unke index numbers ke through access kar sakte hai. Yeh index number zero se shuru hota hai aur ek ek increment hota jaata hai har naye element ke sath.

Isliye, array mein index bahut important hota hai kyunki iske bina array ke elements ko access karna mushkil ho jata hai.

Question 182. Does sort function work for all types of elements ?

Haan, sort function JavaScript mein sabhi prakaar ke elements ke liye kaam karta hai. Yeh function arrays ko sort karne ke liye use kiya jaata hai aur default tareeke se lexicographically (dictionary order) sort karta hai. Lekin agar aap chahte hain ki ismein apna khud ka sorting kaam kare to aap ek compare function bhi provide kar sakte hain.

Question 183. When to pass a sorting function sort function ?

Javascript mein, agar aap ek array ko sort karna chahte hain toh aapko sort() function ka istemal karna padega. Lekin iss function mein aapko ek sorting function bhi pass kar sakte hain jo aapke dwara di gai logic ke hisaab se array ko sort karega.

Jab aapka array simple data types (numbers or strings) se ho toh aapko koi special sorting function pass karne ki zaroorat nahi hai. Lekin agar aapka array complex objects se ho jaise ki arrays of objects ya arrays of arrays toh aapko apni khud ki sorting function banani hogi jo ki aapki specific requirements ko follow karegi.

Iss tarah se, sorting function pass karne ki zarurat tab hoti hai jab default sorting method aapki zarooraton ko pura nahi karti hai.

Question 184. Difference between push and unshift, pop and shift ?

"Push" aur "Unshift" dono JavaScript Array methods hai jo list ke end me naye items ko add karte hai.

Lekin "Push" method list ke end me item ko add karta hai, jabki "Unshift" method list ke beginning me item ko add karta hai.

Ab baat karte hai "Pop" aur "Shift" methods ki. Dono bhi JavaScript Array methods hai jo list se items ko remove karte hai.

"Pop" method list ke last item ko remove karta hai, jabki "Shift" method list ke first item ko remove karta hai.

Question 185. Difference between slice, splice ?

Slice aur Splice dono Javascript ke array methods hain.

Slice() method array ka ek part copy karta hai, yaani ki ek naya array banata hai. Original array modify nahi hota hai. Example:

let fruits = ["apple", "banana", "mango", "kiwi"];
let newFruitsArr = fruits.slice(1, 3);
console.log(newFruitsArr); // Output: ["banana", "mango"]
console.log(fruits); // Output: ["apple", "banana", "mango", "kiwi"]

Splice() method bhi array ka part remove ya add karta hai, lekin ismein original array mei changes hota hai. Ismein hum specified index se start karke, number of elements remove kar sakte hain, ya phir kuch elements add kar sakte hain. Example:

let fruits = ["apple", "banana", "mango", "kiwi"];
fruits.splice(1, 2, "orange", "watermelon");
console.log(fruits); // Output: ["apple", "orange", "watermelon", "kiwi"]

Question 186. What is Node in javascript? What is HTMLCollection? What is Element?

Node javascript mein ek object hai jise DOM tree ke har element ko represent karne ke liye use kiya jata hai. Yeh DOM tree HTML tag, text, attributes, aur even listeners ko bhi represent karta hai.

HTMLCollection ek DOM collection hoti hai jisme webpage ke saare elements stored hote hai, jaise ki divs, images, forms, etc. Ismein specific element ko access karne ke liye index number ka use kiya ja sakta hai.

Element DOM tree ke har single part ko represent karta hai, jaise ki div, p, span, etc. Aur ismein specific element ke saath interact karne ke liye bahut saare properties aur methods available hote hai.

Question 187. Difference between forEach, for of loop in javascript?

JavaScript mein forEach aur for of loop dono hi loops hain jo arrays ke saath kaam karte hain.

forEach() loop mein hum ek function ko array ke har element ke liye chalate hain jisse hum un sabhi elements ko individual basis pe access kar sakte hain. forEach loop ka use tab tak kiya jata hai jab humein array ke har element pe iterate karna ho.

for of loop ka use bhi array ke elements pe iterate karne ke liye kiya jata hai. Yeh loop ek iterable object (jaise array) ke saath kaam karta hai aur usmein diye gaye variable ko array ke har element ke liye assign karta hai. Is loop mein humein array ke current element ko access karne ke liye index ya counter ki zaroorat nahi hoti, isliye yeh forEach loop se zyada readable aur concise hota hai.

Toh, in summary: forEach loop ka use har element ke liye kuch execute karne ke liye kiya jata hai, jabki for of loop ka use ek iterable object ke saath kaam karne ke liye kiya jata hai aur yeh forEach loop se zyada readable hota hai.

Question 188. Difference between map, filter and reduce function in javascript?

Map, filter aur reduce functions JavaScript mein bohot use kiye jaate hai. In functions ka use array manipulation ke liye hota hai.

Map function kisi array ke har element ko iterate karke ek naya array return karta hai jismein original array ke sabhi elements ke values transform kiye hote hai.

Filter function bhi kisi array ke har element ko iterate karta hai aur usmein se kuch elements ko choose kar ke unhein ek naya array mein daalta hai jo sirf selected elements ke values se populate hota hai.

Reduce function iterable object (jaise array) mein se value ko ek single output value mein combine karta hai. Reduce function ke paas do arguments hote hai: accumulator aur current value. Accumulator mein reduce function ke previous iterations ke result ki value save rehti hai aur current value mein abhi iterate kiye hue element ki value hoti hai.

In summary:

  • Map function: Har element ko transform karke naya array return karta hai.
  • Filter function: Kuch elements ko select karke unhein ek naya filtered array mein daalta hai.
  • Reduce function: Iterable object ke saare elements ko accumulate karke single output value return karta hai.

Question 189. Difference between some and every in javascript?

"some" aur "every" dono JavaScript mein arrays par istemal hone wale functions hain. "some" array ke elements mein se kuch ek condition ko satisfy karte hain toh true return karta hai, jabki "every" saare elements ko satisfy karte hue hi true return karta hai.

Jaise ki, agar humein ek array ke elements mein se koi bhi even number dhundhna hai toh hum "some" function ka use karenge, jiska code is tarah se hoga:

array.some(element => element % 2 === 0);

Yadi koi bhi even number milta hai toh yeh true return karega.

Aur agar humein chahte hain ki saare elements even numbers hon toh hum "every" function ka use karenge, jiska code is tarah se hoga:

array.every(element => element % 2 === 0);

Yadi saare elements even numbers hai toh yeh true return karega.

Mujhe ummeed hai ki aapko samajh mein aa gaya hoga!

Question 190. Difference between flat and flatMap in javascript?

Flat aur flatMap dono JavaScript mein arrays ke saath use kiye jaane wale functions hai. Flat ek single-level array ko generate karta hai, yaani ki usmein agar kisi bhi element ke andar bhi array hai toh wo bahar nikal kar single-level array ban jata hai.

FlatMap ek high-order function hai jo ek naya array generate karta hai, jismein har ek element ko ek function ke through map karke phir unhein ek single-level array mein flat kar deta hai. Iska matlab ye hai ki agar aapke original array ke kuch elements khud bhi arrays hain toh flatMap inhein unn nested arrays se bahar nikalkar ek single-level array mein convert kar dega.

Jaise ki ek hotel ka elevator ho sakta hai, jismein aapko ek floor se dusre floor tak le jata hai. Flat ek single floor hai jahaan se aapko lift lekar kisi dusre floor par nahi pahunchata hai. Lekin flatMap ek multi-floor building ke elevator ki tarah hai jo aapko ek floor se dusre floor tak lekar jata hai, saath hi apke liye alag-alag floors par available resources ko combine karke ek naya set bana deta hai.

Question 191. What is an Object in javascript?

JavaScript mein, ek Object ek data structure hai jo ki properties aur methods se bhara hota hai. Har object apne aap mein ek unique entity hota hai jiske paas khud ka state aur behavior hota hai.

Agar hum samjhane ki koshish karein toh, Object ek aisa container hai jismein hum various cheezein rakh sakte hain jaise ki variables, functions, arrays etc. Jaise ki har container ke andar alag-alag cheeze hote hain, Object mein bhi har ek property aur method alag hota hai.

For example, agar hum ek car ke baare mein soch rahe hain toh uska color, model number aur speed property ho sakti hai aur uske chalane, rokne aur park karne jaisi methods ho sakti hai. Yeh sabhi car object ke part hote hain aur inki madad se hum car ke behavior aur state ko control kar sakte hain.

Question 192. Difference between object literal and new object in javascript?

Object literal aur new object mein farak yeh hai ki object literal ka use humein ek existing object ke saath properties aur methods declare karne ke liye karna padta hai jabki new object create karne ke liye 'new' keyword aur Object constructor ka use karna padta hai. Object literal mein humein direct keys aur values define karne hote hai, jaise {key1: value1, key2: value2}, jabki new object ke case mein humein pehle se hi define ki gayi properties ko modify ya extend karne ke liye use karna padta hai.

Question 193. Difference between dot and bracket annotation in javascript?

Brackets notation aur dot notation dono tarah se JavaScript mein object ki properties ko access karne k liye istemaal kiye jaate hain. Dot notation mein hum object ka naam likhte hain, phir uske baad ek dot (.) lagakar property ka naam likhte hain. Jaise ki:

var person = {
  name: "John",
  age: 30
};

console.log(person.name); // Output: John
console.log(person.age); // Output: 30

Brackets notation mein hum object ko square brackets ke andar likhte hain aur phir uske baad property ka naam likhte hain. Jaise ki:

var person = {
  name: "John",
  age: 30
};

console.log(person['name']); // Output: John
console.log(person['age']); // Output: 30

Brackets notation ka fayda yeh hai ki ismein variable ka istemaal bhi kiya jaa sakta hai jisse dynamic property access kiya ja sakta hai. Jaise ki:

var prop = 'name';
var person = {
  name: "John",
  age: 30
};

console.log(person[prop]); // Output: John

Question 194. Explain Object.keys, Object.values and Object.Entries function in javascript?

Object.keys, Object.values aur Object.entries functions JavaScript mein hai, jo objects ke properties ko access karne mein madad karta hai.

Object.keys function object ke properties ke keys ko array ke form mein return karta hai. Iska upyog object ke saare keys ki list banane mein kiya jata hai.

Object.values function object ke properties ke values ko array ke form mein return karta hai. Iska upyog object ke saare values ki list banane mein kiya jata hai.

Object.entries function object ke key-value pairs ko array ke form mein return karta hai. Ye function object ke properties ke saath saath uske key-value pairs bhi retrieve karne ke liye istemal kiya ja sakta hai.

Question 195. What is Set in javascript?

Set ek tarah ka collection hai jisme koi bhi value sirf ek baar hi shamil ho sakti hai. Yani ki agar aap ek set mein kisi value ko do baar add karne ki koshish karte hai toh wo sirf ek baar hi shamil hoga. Set mein values ko order ke hisab se store nahi kiya jaata hai, isliye unhe access karne ke liye index ka prayog nahi kiya jaata hai. Set mein values ko add, delete aur iterate karne ke liye alag-alag methods uplabdh hote hai.

Question 196. What is weakSet in JavaScript in javascript?

WeakSet ek JavaScript data structure hai jismein sirf references store hote hain, aur yeh weak reference ka use karta hai. Weak reference ka matlab hai ki agar ek object ka koi strong reference nahi hai to woh garbage collection ke liye eligible ho jaata hai.

Ismein kuch important methods hote hain jaise add(), has(), and delete() jo ki Set mein bhi hote hain. Lekin ek choti si difference hai ki WeakSet mein objects ke alawa kuch bhi store nahi kar sakte.

WeakSet ka use un cases mein kiya jaata hai jab hume kisi object ko memory leak se bachana hota hai ya fir hum chahte hain ki agar koi object garbage collected ho jaaye to uska reference automatically WeakSet se remove ho jaaye.

Question 197. How to print set values using for of loop in javascript?

Set ek data type hai jo javascript mein multiple unique values store karta hai. Set ke values ko for of loop se print karna bahut hi saral hai. Hum for of loop ka upyog kar sakte hain aur usmein set ke variable ko assign kar sakte hain. Fir hum set ke har ek value ko print kar sakte hain.

Neeche diye gaye code snippet mein iska udaharan diya gaya hai:

// Ek set banate hain
const mySet = new Set([1, 2, 3]);

// For of loop ka upyog karke set ke values ko print karte hain
for (let value of mySet) {
  console.log(value);
}

Is code mein, sabse pehle humne const keyword ka upyog karke mySet naam ka ek set banaya hai. Fir humne for of loop ka upyog kiya hai aur value variable ko mySet ke saath assign kiya hai. Iske baad hum console.log() function ka upyog karke set ke har ek value ko print kar sakte hain.

Ummid hai yeh samajhne mein madad milegi!

Question 198. How to create set to Array in javascript?

Set ko array mein convert karne ke liye, hum Set object ki built-in method "Array.from()" ka upyog kar sakte hain. Is method ko Set object ke saath use karke ek new array create kiya ja sakta hai jismein Set ke saare elements shamil ho jaayenge.

Iska syntax is tarah hota hai:

let mySet = new Set(['a', 'b', 'c']);
let myArray = Array.from(mySet);

Yahan, mySet ek Set object hai jismein 3 elements ('a', 'b', 'c') hain. Hum Array.from() method ka upyog karke mySet se ek new array myArray create kar rahe hain.

Ab myArray mein Set ke saare elements shamil honge aur yeh array output ke roop mein istemaal kiya jaa sakta hai.

Question 199. What is Map ?

Map JavaScript mein ek built-in data structure hai jo key-value pairs ko store karta hai. Map mein, keys aur values dono kuch bhi ho sakte hain, jaise ki string, number, boolean, object, function, ya phir undefined.

Map mein har key ke corresponding value ko access karne ke liye, get() method ka istemal kiya jaata hai. Iske alawa, set(), delete() aur has() jaise useful methods bhi available hote hain.

Ek aur important feature of Map yeh hai ki ismein elements ke order maintain kiya jaata hai, jo ki Objects ke saath nahi hota hai.

Question 200. What is weakMap in JavaScript ?

WeakMap ek JavaScript object hai jo key-value pairs ko store karta hai. Ismein keys ka reference weak hota hai, yani agar keys ko koi dusra reference nahi karta to unhe garbage collector delete kar sakta hai. WeakMap ke values bhi objects ho sakte hain. Ye use cases mein helpful hota hai jahan keys ya values ko dynamically add aur remove kiya jata hai aur memory leaks se bachna chahte hain.

Question 201. What is chaining in Map ?

Map mein chaining ko yeh kehte hai ki hum multiple operations ko ek hi line mein perform karein, jaise ki hum apne necklace mein chain ki tarah ek ke baad ek hote hue multiple chains lagaate hai. Iss tarah se chaining karne se hume code likhne mein kaafi time aur effort bach jaata hai.

For example:

const numbers = [2, 4, 6, 8, 10];

const result = numbers.map(num => num * 2)
                      .filter(num => num > 5)
                      .reduce((acc, num) => acc + num, 0);

console.log(result); // Output: 40

Iss code mein humne pehle map() function ka use kiya hai jo numbers array ke saare elements ko double kar deta hai. Uske baad humne filter() function ka use kiya hai jo un numbers ko filter karta hai jo 5 se bade hai. Final mein, reduce() function ka use kiya hai jo filtered numbers ka sum calculate karta hai. Sabhi operations ko chaining ke through perform kiya gaya hai.

Question 202. How the map will behave if we use an Array as key ?

Agar hum JavaScript mein ek Array ko key ke roop mein use karte hain to map ki vyavastha kaise rahegi?

Jab hum ek Array ko map ke key ke roop mein use karte hain, tab ye Array object ka reference value hi key ke roop mein store hota hai. Lekin, ismein ek samasya hoti hai- agar hum ek naya Array banakar uski values ko pehle wale Array se match karte hain, to phir bhi wo do alag Array objects hote hain aur unke reference values bhi alag hote hain. Isliye, humare paas ek unique key nahi hota aur map sahi tarike se work nahi karta. Isliye hamesha primitive data types jaise string, number ya symbol ko use karna chahiye map ke key ke roop mein.

Question 203. How to convert Map to Array ?

JavaScript mein, Map ek collection hai jisme key-value pairs hote hain. Agar aap Map ko Array mein convert karna chahte hain, to aap Map ke entries() method ka use kar sakte hain.

Yah entries() method ek iterable object (like array) return karta hai, jise aap Array.from() method ka use karke Array mein convert kar sakte hain.

Niche diye gaye code snippet ko dekhein:

// Ek Map banana:
const myMap = new Map([
  ['one', 1],
  ['two', 2],
  ['three', 3]
]);

// Map ko Array mein convert karne ke liye:
const myArray = Array.from(myMap.entries());

// Output:
// [ ["one", 1], ["two", 2], ["three", 3] ]

Is code mein, humne pahle ek Map banaya, fir usko Array mein convert kiya using entries() and Array.from() methods.

Mujhe ummid hai ki yah samajh mein aaya hoga!

Question 204. How to convert Array to Map ?

Array ko Map mein convert karne ke liye, hum JavaScript ke Map constructor ka upyog kar sakte hain. Map constructor ek naya khali Map banata hai, jisme hum pratyek array element ko ek key-value pair ke roop mein add kar sakte hain.

Niche diye gaye code snippet mein, hum ek Array bana rahe hain jiska naam hai myArray, aur phir hum usko Map mein convert kar rahe hain.

// Example Array
let myArray = [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']];

// Convert Array into Map
let myMap = new Map(myArray);

Yahan par, humne new Map(myArray) ka upyog kiya hai, jisse ek naya Map bana diya gaya hai jismein hum myArray ke sabhi elements key-value pairs ke roop mein shamil kar chuke hain.

Iske baad, hum myMap variable mein naye Map ko store kar rahe hain. Now, myMap map mein ab key1, key2, and key3 keys hongi, aur unke corresponding values yeh honge: value1, value2, and value3.

Question 205. How to convert Object to Map ?

Javascript mein Object ko Map mein convert karne ke liye, hum ye steps follow kar sakte hai:

  1. Sabse pehle, ek khali Map object create karein jiske andar hum Object ko store karenge.

  2. Fir, Object.entries() method ka use karein jisse hum Object ke key-value pairs ko array of arrays mein convert kar sakte hai.

  3. Ab is array of arrays ko iterate karke har ek key-value pair ko Map object mein set karein.

Yeh pura process humari code ke is tarah se dikhega:

// Object create karein
const myObj = {name: 'John', age: 30};

// Khali Map object create karein
const myMap = new Map();

// Object entries ko array of arrays mein convert karein
const objEntries = Object.entries(myObj);

// Array of arrays ko iterate karke Map object mein set karein
for(const [key, value] of objEntries) {
  myMap.set(key, value);
}

console.log(myMap); // Output: Map(2) { 'name' => 'John', 'age' => 30 }

Is code mein humne pehle ek Object myObj create kiya, fir usse convert karke ek khali Map object myMap create kiya. Fir, hum Object.entries() method ka use karke myObj ke key-value pairs ko array of arrays objEntries mein convert kiya. Finally, objEntries ko iterate karke har ek key-value pair ko myMap object mein set kiya.

Iss tarah se hum Javascript mein Object ko Map mein convert kar sakte hai.

Question 206. How to iterate map using foreach and for of loop ?

Foreach loop ka upyog karke hum map ke har element par access kar sakte hain. For example:

let myMap = new Map();
myMap.set("name", "John");
myMap.set("age", 30);

// foreach loop
myMap.forEach((value, key) => {
  console.log(key + " = " + value);
});

For of loop se bhi hum map ke elements par access kar sakte hain. For example:

let myMap = new Map();
myMap.set("name", "John");
myMap.set("age", 30);

// for of loop
for (let [key, value] of myMap) {
  console.log(key + " = " + value);
}

Dono hi loops ka upyog karke hum map ke sabhi elements ko aasani se iterate kar sakte hain.

Question 207. What is OOPs ?

OOPs stands for Object-Oriented Programming, which is a programming paradigm in which we create objects that contain both data and functionality.

OOPs ka matlab hai Object-Oriented Programming, jo ek programming paradigm hai jisme hum objects banate hain jisme data aur functionality dono shamil hote hain.

Question 208. What are the 6 priniciples of OOPs?

OOPs ke 6 mukhya tatva hai: Encapsulation, Inheritance, Polymorphism, Abstraction, Class, aur Object.

  1. Encapsulation - Ye ek tarika hai jis se hum kisi bhi data ko private rakh sakte hai aur use sirf us class ke andar hi access kar sakte hai. Isse data ki security bhi badhti hai.

  2. Inheritance - Iska matlab hai ki ek class dusri class ke features ko inherit kar leti hai. Jaise ki ek car class sedan ya hatchback class ke functions ko inherit kar sakti hai kyunki ye dono cars hai.

  3. Polymorphism - Iska matlab hai "multiple forms". Jaise ki ek function alag alag tareeko se kaam kar sakta hai, jaise ki ek add() function number ko bhi add kar sakta hai aur string ko bhi concatenate kar sakta hai.

  4. Abstraction - Iska matlab hai ki hum complex cheezo ko simple tareeko se represent karte hai. Jaise ki hum car class me speed() function banakar car ki speed ko calculate kar sakte hai, lekin jaise hi hum function call karenge, hume details nahi pata chalegi ki ye function kaise kaam karta hai.

  5. Class - Ye ek blueprint hota hai, jisme object ke attributes aur functions define hote hai.

  6. Object - Ye class ka instance hota hai, jise hum class ke attributes aur functions ko access karne ke liye use karte hai.

Question 209. Explain below topics and give one real life example of each topic Class, Object, Encapsulation, Abstraction, Inheritance, Polymorphism ?

Class - Ek blueprint ya template hota hai jo humare objects ke bare mein batata hai. Ismein hum properties aur methods define karte hain. Jaise ki ek car class mein, hum speed, color, model number jaise properties aur accelerate, brake, gearChange jaise methods define kar sakte hain.

Object - Class ka instance jo real world mein exist karta hai. Hum class ka use karke objects create karte hain. Jaise ki agar hum car class se ek object create karte hain, toh woh object ek specific car ko represent karega.

Encapsulation - Yeh ek concept hai jismein hum class ke variables aur methods ko private rakhte hain, taki unhe access karne ke liye class ke bahar wale code ko uske public methods ka use karna padta hai. Yeh security aur abstraction ko improve karta hai.

Real life example: ATM machine, where we insert a card and enter a PIN number to access our bank account details and perform transactions. The details and transactions are encapsulated inside the ATM machine and are not accessible directly.

Abstraction - Yeh ek process hai jismein hum complex systems ko simple banate hain. Abstraction hume sirf necessary information dikhata hai aur unnecessary details ko chupa deta hai. Jaise ki car class mein, hum user ko sirf car start/stop karne, speed control karne jaise basic actions provide karte hain, bina usko engine, piston, gearbox jaise internal details dikhaye.

Inheritance - Yeh ek principle hai jismein hum ek class ko dusre class se inherit karte hain. Inherited class ko "child" class aur jis class se inherit kiya jaata hai use "parent" class kehte hain. Jisse child class apne parent class ke properties aur methods ko use kar sakte hain aur usmein apni specific behavior add kar sakte hain.

Real life example: Suppose we have a "Vehicle" class, and we create two child classes "Car" and "Truck". Both the car and the truck inherit the properties of the Vehicle class, such as the number of wheels and the type of fuel they use, but each has its own specific behavior, such as the maximum speed or cargo capacity.

Polymorphism - Yeh ek concept hai jismein hum ek method ko multiple ways mein define karte hain, jisse same method ko alag alag child classes mein alag alag tariko se implement kiya ja sake. Jaise ki agar hum "playSound" method ko animal, bird, aur insect classes mein define karte hain, toh har class mein sound ki nature alag hogi.

Real life example: A media player can play different types of media files such as audio, video, and images. Each file format has its own way of being played, but they all use the same "play" method in the media player. This is an example of polymorphism.

Question 210. How to inheritance works in JavaScript?

In JavaScript, inheritance is achieved through the prototype chain. Yeh aise kam karta hai ki har object ke paas ek prototype hota hai, jisko wo apne properties or methods ko inherit (yani apne se pehle wale object se uthana) kar leta hai. Jab hum kisi object ki property ya method ko access karte hai, to JavaScript sabse pehle us object ke andar dekhta hai ki vo property/method uske andar hai ya nahi. Agar nahi hai, to vo object apne prototype ke andar dekhta hai, or chahe jitni bhi levels tak prototype chain ho, dhundta rahta hai jab tak property/method mil jaaye.

Inheritance ko banane ke liye hum parent class ka ek object create karte hai or uska prototype child class ke prototype ke saath set karte hai. Iske baad hum child class ka ek object create kar sakte hai, jo apne prototype ke andar parent class ke methods aur properties ko inherit karega. Hum chahe to child class ke andar parent class ke methods ko override bhi kar sakte hai.

Question 211. What is prototypal inheritance ?

Prototypal inheritance ek concept hai Javascript mein jismein ek object dusre object ke properties aur methods ko inherit karta hai. Ye inheritance tree ki tarah hota hai jahan har object apne parent object se kuch properties aur methods inherit karta hai.

Jab hum ek object create karte hain, to uske paas ek prototype property hoti hai jo use us object ke parent object ke saath link karti hai. Agar hum koi property ya method access karna chahte hain jo us object mein available nahi hai, to Javascript engine us property ko prototype chain ke through search karta hai aur agar woh us property ko parent object mein paata hai to use wahan se inherit kar leta hai.

Ye concept bahut powerful hai kyunki isse hum objects ke saath reusability achieve kar sakte hain aur code duplication ko kam kar sakte hain.

Question 212. What are the different ways of doing prototypal inheritance ?

JavaScript mein prototypal inheritance karne ke kuchh tarike hain, jaise ki:

  1. Prototype Chain: Sabse common tareeka hai jismein hum ek constructor function banate hain aur uske prototype object ke properties ko use karte hain. Isse child object inherit karta hai apne parent object ke properties aur methods.

  2. Object.create(): Ye method ek parent object ko liya jaata hai aur uska reference lekar ek naya object create karta hai jiske prototype parent object ke saath set kiya jaata hai. Isse bhi child object parent object ke properties ko inherit karta hai.

  3. Constructor Inheritance: Ismein ek parent object se ek naya object banaya jaata hai aur parent object ke properties ko child object ke constructor function mein set kiya jaata hai. Lekin is method mein parent object ka constructor function inheriting nahi hota, isliye ismein kuch limitations ho sakti hain.

In sabhi tareeko mein hum ek object se dusre object ko inherit karte hain aur inheritance ke through parent object ke properties aur methods ko child object mein use karte hain.

Question 213. What is Constructor function ?

Constructor function JavaScript mein ek special type ka function hota hai jo objects ko create karne ke liye use kiya jata hai. Is function ke through hum class-like objects bana sakte hai. Jab bhi hum Constructor function ko call karte hai, toh naya object create hota hai jo us function ke prototype property se inherit karta hai. Yeh function generally capital letter se start hota hai jaise 'Person' ya 'Car'.

Question 214. What is ES6 classes ?

ES6 classes JavaScript mein ek tarah se blueprint hota hai, jiske upyog se hum objects ko banate hain. Isse pehle, hum constructors aur prototypes ka istemaal karte the lekin ES6 classes ne is process ko asaan aur straightforward bana diya hai. ES6 classes mein hum properties (guna), methods (tareeke) aur constructors define kar sakte hain, jo ki phir objects ke liye use kiye jaate hain. Ye code ko modular aur easily maintainable banata hai.

Question 215. What is Object.create ?

Object.create() ek JavaScript method hai jo naye objects ko banane ke liye istemal hota hai, jiske paas already existing object ko as a prototype use karne ki suvidha hoti hai. Isse hum "inheritance" ko achieve kar sakte hain aur code ko modular aur reusable bana sakte hain.

Question 216. What is prototypal chain ?

Prototypal chain ek concept hai JavaScript mein jismein har object apne prototype se inherit karta hai. Prototype ek blueprint hota hai jo ek object ke properties aur methods ko define karta hai. Agar kisi object mein koi property ya method nahi hota hai, to JavaScript usko uske prototype se dhundta hai.

Yeh ek tarah ka inheritance system hai jismein agar hum kisi object ke properties ya methods ko access karna chahte hain aur woh object unhe directly contain nahi karta hai, to JavaScript unhe uske prototype object se lekar aata hai. Aur agar us prototype object mein bhi property ya method nahi hota hai, to JavaScript usko uske parent prototype object se dhundta hai aur yeh process aise hi chalta rehta hai jab tak ki root prototype object (Object.prototype) tak pahunch nahi jaata.

Iss tarah se, prototypal chain JavaScript mein multiple objects ke beech inheritance establish karne ki allow karta hai.

Question 217. What is setters and getters in ES6 classes ?

Setters aur getters ES6 classes mein Javascript ke aham tareen features hain jo ki object-oriented programming ko asan banate hain. Setters aur getters ki madad se ham apne class ke properties ko private rakh sakte hain aur unhe get aur set karne ke liye methods ka use kar sakte hain.

Setters, kisi property ki value ko set karne ke liye use kiye jate hain, jabki Getters property ki value ko retrieve karne ke liye use kiye jate hain. Setter method mein, ek parameter pass kiya jata hai jo property ki value ko set karne ke liye use hota hai.

Yah feature aapko class ke bahar se property ko access nahi karne deta hai, isse aapki code ki security bhi badhti hai. Iske alawa, setters aur getters ki madad se hum validation aur error handling bhi kar sakte hain.

Question 218. What will happen if we give same name of property of an object and setter and getter ?

Agar hum ek object ki property aur uske setter ya getter ko same naam denge toh JavaScript me kya hoga?

Yeh ek aam samasya hai jisme hum setter aur getter ko usi naam se define karte hai jis naam ka object ki property bhi hai. Is situation me, jab bhi hum us property ka value change karenge, setter method call hoga aur agar hum us property ka value read karna chahte hai to getter method call hoga. Lekin, yadi hum setter ya getter ko call karenge to woh infinite loop me fas jayega kyunki property and methods dono ka naam same hai. Aise cases me, hume either property name ya methods name ko change karna padega taaki dono ke beech conflict na ho.

Question 219. What is static function in ES6 classes ?

ES6 classes mein static function woh function hota hai jo class ke instance se call nahi kiya ja sakta hai. Yeh function sirf class ke through access ho sakta hai. Jaise ki hum class ke properties ko access karte hain, vaise hi static function ko bhi access karte hain. Static function ka upyog utility functions ya general helper functions banane ke liye kiya jata hai jo sirf class ke level par hi kaam karte hain aur instance level par nahi.

Question 220. How to implement inheritance in Constructor functions, ES6 classes and Object.create ?

Inheritance ka matlab hota hai ek object dusre object se properties or methods ko inherit karna. JavaScript mein inheritance implement karne ke liye hum Constructor Functions, ES6 Classes aur Object.create ka use kar sakte hain.

Constructor Functions mein inheritance ke liye, hum prototype object ka use karte hain. Hum parent constructor function ke prototype object ko child constructor function ke prototype object ke duara access karke inheritance create karte hain.

ES6 Classes mein extends keyword ka use karke hum inheritance create karte hain. Child class extends parent class ko kar leta hai aur usme parent class ke functions aur properties bhi use kar sakte hain.

Object.create() method mein hum parent object ko __proto__ property ke duara child object ke prototype chain mein add kar dete hain. Isse child object parent object ke properties aur methods ko inherit kar leta hai.

In sab tareeko mein hum inheritance ke zariye code reuse kar sakte hain aur apni classes ko modular banate hain.

Question 221. How to implement encapsulation? How to make fields protected ?

Encapsulation ka matlab hota hai ki data aur uske sath sath uske functions ko ek saath rakhe jaye taki vo secure ho. Jaise ki hum apne phone ke apps ke data ko ek saath rakhte hain, taki koi dusra insaan unhe access na kar sake.

Javascript me fields ko protected banane ke liye, hum 'underscore' (_) ka use kar sakte hain jisse ye fields private ho jayenge, yani sirf us class ke andar hi access kar payenge. Ye humko aur bhi security provide karta hai.

Jaise:

class MyClass {
  _myField = "My Protected Field"; // underscore se field ko protect kiya gaya hai

  getMyField() {
    return this._myField; // _myField ko is class ke andar hi access kar sakte hain
  }
}

Question 222. How to make fields and functions private ?

JavaScript mein fields aur functions ko private banane ke liye, hum "closure" ka upyog kar sakte hain. Ek closure ek function hai jo ek object ko return karta hai, jismein saare private fields aur functions hote hain. Inn private elements ko access karne ke liye, public methods ka upyog kiya jaata hai.

Iske liye, hum ek IIFE (Immediately Invoked Function Expression) bana sakte hain, jismein hum uss object ko return karte hain jo hamare private fields aur functions ko contain karta hai. Public methods ko iske baad add kiya jaata hai, jo iss object ke properties hote hain aur private elements tak pahunchne ke liye inka upyog kiya jaata hai. Iss tarike se, humare code kaafi organized aur maintainable ho jaata hai.

Jaise:

var myModule = (function() {
  var privateField = "privateValue";

  function privateFunction() {
    console.log("This is a private function");
  }

  return {
    publicMethod: function() {
      console.log("This is a public method that can access the private members");
      console.log(privateField);
      privateFunction();
    }
  };

})();

myModule.publicMethod(); // Output: This is a public method that can access the private members
                         //         privateValue
                         //         This is a private function

Iss code mein, myModule ek object hai jo IIFE ke through create kiya gaya hai. Yeh object sirf ek public method publicMethod ko contain karta hai, jo private field privateField aur private function privateFunction ko access kar sakti hai.

Question 223. What is chaining methods? How can we implement the same in ES6 classes ?

Chaining methods ka matlab hota hai ki ek hi line mein multiple functions ko call karna aur uska output ko further use karna. Jaise ki, agar hum ek array ke upar multiple array methods ko implement karna chahte hai toh hum chaining methods ka use kar sakte hai.

ES6 classes mein chaining methods ko implement karne ke liye, humein apni class ke har method ke ant mein 'return this' likhna padta hai. Isse, hum apne objects ko modify karke unhein further methods ke liye chain kar sakte hai.

Jaise ki, niche diya gaya example dekh lein:

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
    this.speed = 0;
  }

  setMake(make) {
    this.make = make;
    return this;
  }

  setModel(model) {
    this.model = model;
    return this;
  }

  setYear(year) {
    this.year = year;
    return this;
  }

  accelerate() {
    this.speed += 10;
    return this;
  }

  brake() {
    this.speed -= 10;
    return this;
  }
}

const car1 = new Car('BMW', 'X5', 2021);
car1.setMake('Audi').setYear(2022).accelerate().brake();
console.log(car1);

Is example mein, humne Car class banayi hai jismein setMake(), setModel(), setYear(), accelerate() aur brake() function hai. Har function ke ant mein 'return this' ka use kiya hai jo chaining methods ke liye important hai.

Humne car1 object create kiya hai aur usko setMake(), setYear(), accelerate() aur brake() methods ke through modify kiya hai. Yeh sabhi methods ko humne ek hi line mein likha hai jo chaining methods ka example hai.

Output mein, hum dekh sakte hai ki car1 object ke make, year aur speed values change ho chuke hai.

Question 224. What is Synchronous and Asynchronous ?

Synchronous aur Asynchronous JavaScript me difference hai, jisme Synchronous ka matlab hota hai ki ek kaam tab tak wait karega jab tak wo complete na ho jaye, jabki Asynchronous ka matlab hota hai ki ek kaam ko shuru karne ke baad dusre kaam ko bhi shuru kar diya ja sakta hai, yani dono kaam ek saath chalte hai.

Synchronous code line by line execute hota hai, jisse agar koi kaam time-consuming ho toh pura process slow ho jata hai. Jabki Asynchronous code me kaam background me run karta hai, jisse doosre kaam bhi continue chalte rahte hai.

Ek example se samjhein toh, imagine kijiye aapke paas ek chai banane wali machine hai aur machine me 3 steps hai - pani daalna, chai bag daalna, doodh daalna. Ab synchronous code se, aap sabse pehle pani daalenge, phir chai bag daalenge aur fir doodh daalenge. Lekin agar pani garam hone me time lag raha ho, toh aapko chai banana ke liye intezaar karna hoga.

Jabki asychronous code me, aap pani daalte hi chai bag daal sakte hai aur fir doodh daal sakte hai, chahe pani garam ho ya nahi. Isse aap doosre kaamo ko bhi simultaneously kar sakte hai.

Yeh example sirf samajhne ke liye tha, lekin javascript me Synchronous aur Asynchronous kaafi important concept hai, jiske through aap apne code ke performance ko improve kar sakte hai.

Question 225. What is AJAX ?

AJAX ek aise technique hai jismein hum web page ko refresh kiye bina server se data retrieve kar sakte hain. Iska matlab hai ki jab aap kisi website pe kuchh change karte hain, to webpage ko puri tarah se reload nahi kiya jata. Instead of that, sirf uss part ko update kiya jata hai jismein changes hue hain.

Yeh JavaScript ka ek bahut important concept hai jiski madad se website ko faster aur interactive banaaya ja sakta hai. AJAX ka full form hai Asynchronous JavaScript And XML.

Question 226. What is API? Explain topics SOAP API, Rest API, Request, Response, Request Body, Query Param, Path Variable, URL, URI, Data : JSON/XML/Text

API ka matlab hota hai "Application Programming Interface". Ye ek tarika hai jisse ek software application, dusre application ke saath communication kar sakta hai.

SOAP API: SOAP (Simple Object Access Protocol) ek standard protocol hai, jo XML format mein data ko exchange karta hai. Ismein request aur response ko envelopes mein wrap kiya jaata hai.

Rest API: REST (Representational State Transfer) ek architecture style hai, jis mein HTTP requests se kaam liya jaata hai. Ismein data ko URL ke through transfer kiya jaata hai, aur response JSON/XML/Text mein aata hai.

Request: Jab ek client server se kuchh manga hai, to wo request hoti hai.

Response: Server jab client ke request ka response bhejta hai, to use response kehte hain.

Request Body: Jab ek client server ko data send karta hai, to wo data request body ke andar hota hai.

Query Param: Query parameters URLs ke andar hote hain aur typically "?key=value" format mein hotey hain. Ye client ke dwaara specific data ko filter karne ke liye use kiye jaate hain.

Path Variable: Path variable URL mein dynamic values hote hain, jaise "/users/{id}" jahan "id" ek path variable hai.

URL: URL (Uniform Resource Locator) ek unique address hai, jise web browser use karke internet par kisi website tak pahunch sakte hain.

URI: URI (Uniform Resource Identifier) ek identifier hai, jo resource ko uniquely identify karta hai.

Data: Data ek collection hota hai jismein information shamil hoti hai. Data ko commonly JSON/XML/Text format mein represent kiya jaata hai.

Question 227. What is server client architecture in javascript?

Server client architecture ka matlab hai ki jab aap ek website par visit karte hain toh wahan pe aapka computer client hota hai aur woh website jo aapko dikh rahi hai uska data aur code ek server ke taraf se aata hai. Is process ko hum "client-server architecture" kehte hain.

Ab JavaScript me, client-side scripting hoti hai jahan pe aapke browser me scripts chalte hain aur server-side scripting hoti hai jahan pe server ke taraf se code run hota hai. Server side me node.js jaisi technology use ki jati hai jo ki JavaScript par based hai.

Question 228. What is Promise and fetch in javascript?

Promise ek aisa object hai jo asynchronous operations ke liye use kiya jata hai. Jab hum kisi function ko call karte hain jo data fetch karne ya kisi task ko perform karne ke liye time lete hain, tab hum is function ko Promise ke andar rakhte hain. Promise 3 states me ho sakta hai: pending, fulfilled aur rejected.

Fetch ek built-in Javascript method hai jo server se data fetch karne ke liye use kiya jata hai. Isme hum URL ko pass karte hain jiske baare mein data fetch karna hai, phir response ko handle karne ke liye Promise ka use kiya jata hai. Yeh ek modern alternative hai XMLHttpRequest (XHR) object ke liye.

Question 229. How to consume promises in javascript?

Promises ek aisa concept hai jo Javascript mein use hota hai asynchronous programming ke liye. Asynchronous programming ka matlab hota hai ki hum code ko aise likhte hain jismein ek kaam ho raha ho toh dusra kaam bhi chalta rahe, taki application slow na ho.

Promises ka use tab kiya jata hai jab hum kisi task ka result future mein expect kar rahe hote hain aur uske baad us result ke saath kuch aur kaam karna chahte hain. Promises ke 3 states hote hain - Pending (wait kar raha hai), Fulfilled (kaam ho gaya hai) aur Rejected (kuch error ho gaya hai).

Promise ko consume karne ke liye hum .then() aur .catch() methods ka use karte hain. Hum .then() method ka use karte hain jab promise fulfill ho chuka hota hai aur hum uska result access karna chahte hain. Aur .catch() method ka use karte hain jab promise reject ho jata hai aur hum uska error handle karna chahte hain.

Yeh ek example hai:

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.log(error))

Is example mein hum fetch() function ka use kar rahe hain jo ek api call karti hai. Iska response hum .then() method ke through access kar rahe hain. Agar koi error aaye toh hum .catch() method se handle kar lenge.

Question 230.How handle rejected promises ?

Javascript mein rejected promises ko handle karne ke liye, hum .catch() ka use karte hain. Agar promise reject ho jaati hai, toh .catch() ka function call hota hai aur usmein hum error message ya fir koi aur handling kar sakte hain.

Is tarah se, hum apne code mein errors ko handle kar sakte hain aur sahi tarah se program ko chala sakte hain.

Question 231. How to create a Promise ?

Promises ek tareeka hai JavaScript mein async programming ke liye jiske dwara hum code ko non-blocking bana sakte hain.

Ek naya Promise object create karne ke liye, hum new Promise() ka upyog karte hain aur ismein ek function pass karte hain jismein do arguments honge - resolve aur reject.

resolve function tabhi call kiya jaata hai jab async operation successfully complete ho jaati hai. Agar koi error aati hai to reject function ko call kiya jaata hai.

For example:

const myPromise = new Promise((resolve, reject) => {
  // Async operation to be performed
  let result = performAsyncOperation();
  
  if (result) {
    resolve("Success message");
  } else {
    reject("Error message");
  }
});

Yahan performAsyncOperation ek async operation hai jise hum execute kar rahe hain. Agar ye operation successfully complete hoti hai to hum resolve ko call karenge aur agar koi error aati hai to reject ko call karenge. myPromise abhi ek pending Promise hai jo humari async operation ka result hold kar rahi hai.

Hum Promise ko .then() aur .catch() functions ke saath chain kar sakte hain jisse hum success aur error cases ko handle kar sakte hain.

myPromise.then((successMsg) => {
  // Success case handling
  console.log(successMsg);
}).catch((errorMsg) => {
  // Error case handling
  console.log(errorMsg);
});

Yahan hum .then() aur .catch() functions ka upyog kar rahe hain jisse hum success aur error cases ko handle kar sakte hain. Agar async operation successfully complete hoti hai toh .then() function ko call kiya jaata hai aur agar koi error aati hai toh .catch() function ko call kiya jaata hai.

Question 232. What is async/await ?

Async/await ek JavaScript feature hai jo asynchronous code execution ko asaan banata hai. Async keyword se function ko async banaya jaata hai, jise await keyword se call kiya jaata hai.

Jab hum kisi function ke andar await ka use karte hai, tab tak wo function ke execution ko rok deta hai jab tak ki uske saath attached promise resolve ya reject na ho jaye. Isse code readability aur maintainability improve hoti hai.

Iska upyog commonly network requests, file I/O operations aur anya async tasks ke liye kiya jaata hai.

Question 233. How to return values from Async function ?

Async functions in JavaScript return a Promise that resolves to the value returned by the function. You can use the await keyword to wait for the Promise to resolve and get the returned value.

To return values from an async function, you can simply use the return keyword inside the function as you would normally do in synchronous functions. However, since async functions return Promises, you need to wrap the returned value in a resolved Promise using the Promise.resolve() method.

Here's an example:

async function getData() {
  const response = await fetch('https://example.com/data');
  const data = await response.json();
  return Promise.resolve(data);
}

getData().then((data) => {
  console.log(data);
}).catch((error) => {
  console.error(error);
});

In this example, the getData() function returns a Promise that resolves to the JSON data fetched from the URL. The Promise.resolve() method is used to wrap the data in a resolved Promise before returning it.

You can call the getData() function using the then() method to get the returned data, or the catch() method to handle any errors that occur during the execution of the async function.

Question 234. How to run promises in Parallel ?

Promises ko parallel me kaise chalaya ja sakta hai Javascript me?

Promises ko ek saath chalane ke liye, Promise.all() function ka use kiya jata hai. Is function ko ek array of promises diya jata hai aur wo sab promises ko ek saath execute karne ki koshish karta hai.

Example:

const promise1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('Promise 1 complete');
  }, 2000);
});

const promise2 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('Promise 2 complete');
  }, 4000);
});

Promise.all([promise1, promise2])
  .then((results) => {
    console.log(results);
  })
  .catch((error) => {
    console.error(error);
  });

Is example me, do promises "promise1" aur "promise2" create kiye gaye hain jo alag-alag time ke baad resolve ho jayenge. Promise.all() function ko in dono promises ko as an array pass kiya jata hai. Jab dono promises resolve ho jate hain, "then" block execute hota hai aur array me dono promises ke results print ho jate hain.

Question 235. How to do error handling? Explain try catch and finally ?

Error handling ka matlab hota hai ki, aapki code mein koi issue ya error aane par uss issue ko handle karna. Iske liye, JavaScript language mein "try-catch-finally" ka use kiya jaata hai.

Try-Catch-Finally statement ka use karke aap apne code ki errors ko handle kar sakte hain. Yeh ek aisa method hai jismein aap kisi bhi risky code ko try block ke andar rakh sakte hain, aur agar issue aaye to wo catch block ke andar handle kiya ja sakta hai.

Try: Try block mein risky code rakhna hota hai. Agar yahaan par koi exception ya error aata hai, to control flow automatically catch block mein jayega.

Catch: Catch block mein aap error ko identify kar sakte hain aur use handle kar sakte hain. Yeh block tabhi execute hota hai jab try block mein koi error aata hai.

Finally: Finally block mein, aap code likh sakte hain jo hamesha execute hoga chahe try-catch blocks mein koi error aaye ya na aaye. Finally block ka use close karne jaise tasks ya resources release karne ke liye kiya jata hai.

try {
  // yahaan risky code likha jaata hai
} 
catch (error) {
  // error ko handle karne ke liye yahaan code likha jaata hai
} 
finally {
  // yahaan hamesha execute hone waala code likha jaata hai
}

Umeed hai ki ye explanation aapke liye helpful hoga.

Question 236. Explain some of the promise combinators in javascript? Race, allSettled, any ?

Javascript mein kuch promise combinators hote hain jaise:

  1. Race - Ismein multiple promises ko pass kiya jaata hai aur jo bhi promise pehle resolve ho jaata hai, uska result return hota hai. Jaise ki, "Agar ek race shuru hai aur 3 log bhag rahe hain, toh jis insaan ne pehle finish line cross kiya, woh winner hoga".

  2. allSettled - Ismein multiple promises ko pass kiya jaata hai aur saare promises settle hone ke baad (yaani resolved ya rejected hone ke baad) unke results ka array return hota hai. Iss se hum saare promises ke results ko collect kar sakte hain, chahe wo successful hon ya unsuccessful.

  3. any - Ismein multiple promises ko pass kiya jaata hai aur jis bhi promise ka state pehle change hota hai (resolved ya rejected), uska result return hota hai. Agar sabhi promises reject ho jaate hain toh error throw kiya jaata hai. Jaise ki, "Agar 3 log ek race shuru karte hain aur 2log pehle finish line cross karte hain, toh jiske paas zyaada points hain woh winner hoga".

Question 237. What is module in JavaScript?

JavaScript me module ek aisa code block hota hai jo specific functionality ko perform karta hai aur use dusre codes se alag rakhta hai. Ye code reusability aur maintainability ko improve karta hai. Hum modules ke through variables, functions aur objects ko export aur import kar sakte hai. Basically, ye ek jatil program ki choti si unit hoti hai jisko dusre program se use kiya ja sakta hai.

Question 238. How to import and export modules ?

Javascript mein modules import aur export karne ke liye, hum import aur export statements ka use karte hain.

Modules ko import karne ke liye import keyword ka use kiya jata hai. Import statement ke baad aapko module ka naam dena hota hai, jo aapko import karna hai. Iske baad aap module ka path dete huye likhte hain. Jaise ki:

import { functionName } from './module.js';

Yahan functionName ek function ka naam hai, jo hum './module.js' file se import kar rahe hain.

Modules ko export karne ke liye export keyword ka use kiya jata hai. Export statement ke baad aapko variable ya function ka naam dena hota hai, jo aap export karna chahte hain. Jaise ki:

export function functionName() {
  // Function code here
}

Is code se, functionName function ko export kiya ja raha hai, jisse dusre files mein access kiya ja sakta hai.

Iss tareeke se aap JavaScript mein modules ki madad se code ko organize kar sakte hain aur reusability ko badha sakte hain.

Question 239. What is Polyfilling ?

Polyfilling JavaScript ka matlab hai ki aap kisi nayi browser feature ko use karne se pehle uska support purane browsers me bhi kar dete hain. Isse ye benefit hota hai ki aap apni website ke users ko ek consistent experience provide kar sakte hain.

Jaise maan lijiye aapki website kaafi saare users purane versions ke Internet Explorer, jaise IE 8 ya IE 9 use kar rahe hain. Aap chahte hain ki aapki website un sabhi users ke browsers par acche se work kare, lekin aapke paas kuch features hain jo sirf modern browsers me hi work karte hain.

Polyfilling ka use karke aap kisi bhi naye feature ko implement kar sakte hain aur isse purane browsers me bhi us feature ka support ho jata hai. Isse aapki website ke users ko ek better experience milta hai aur woh aapki site ko kisi bhi browser me acche se access kar sakte hain.

Question 240. What is Transpiling ?

Transpiling ek process hai jisme hum ek programming language ka code dusre language ke code mein convert karte hain. JavaScript mein, hum usually ES6 ya ES7 code likhte hain jo ki bahut hi advanced hota hai. Lekin, sabhi browsers abhi tak isko support nahi karte. Isliye, hum Transpiling ka use karte hain taki humara code sahi se work kare har browser par.

Iske liye, hum apne ES6/ES7 JavaScript code ko babel jaise tools ka use karke ES5 (older version of JavaScript) code mein convert karte hain. Ye code different browsers par easily run kar sakta hai aur hume cross-platform compatibility provide karta hai.

Question 241. Difference between polyfilling and transpiling ?

Polyfilling aur Transpiling dono hi JavaScript ke development me important hai.

Polyfilling ka matlab hota hai ki aap kuch aise functionality ko add kar rahe ho jo purane browsers me nahi available hai. Jaise agar aap kisi modern property ka use karte ho, toh polyfill uss feature ko purane browsers me bhi work karne ke liye support karta hai.

Transpiling ka matlab hota hai ki aap kuch aise code ko ek version se dusre version me convert kar rahe ho. Jaise agar aap ES6 syntax ka use karte ho toh transpiler uss code ko ES5 syntax me convert karke purane browsers me bhi chalane ke liye ready kar deta hai.

Toh Polyfilling aur Transpiling dono hi bahut useful hai aur inko sahi tarike se use karna important hai, especially jab cross-browser compatibility ki baat aati hai.