JS टाइप कन्वर्ज़न
In this page:
String(value);
Number(value);
Boolean(value);
parseInt(string);
Implicit बनाम Explicit Conversion
Implicit conversion, जिसे coercion भी कहते हैं, तब अपने आप होता है जब JavaScript को matching types की values चाहिए होती हैं, जैसे + या == में, जबकि explicit conversion तब है जब आप खुद जानबूझकर Number() जैसा कोई function कॉल करके किसी value को बदलते हैं।
उदाहरण: Implicit vs Explicit Conversion
console.log("5" + 1); // "51" - implicit coercion
console.log(Number("5") + 1); // 6 - explicit conversion
Number(), String(), और Boolean()
Number() किसी value को उसके numeric equivalent में बदलता है, String() किसी value को text में बदलता है, और Boolean() JavaScript के truthy और falsy rules के आधार पर किसी value को true या false में बदलता है।
उदाहरण: Number(), String(), and Boolean()
// Print `Number("42")` to the console
// Print `Number("42")` to the console
console.log(Number("42"));
// Print `String(42)` to the console
// Print `String(42)` to the console
console.log(String(42));
// Print `Boolean(0)` to the console
// Print `Boolean(0)` to the console
console.log(Boolean(0));
// Print `Boolean("hello")` to the console
// Print `Boolean("hello")` to the console
console.log(Boolean("hello"));
parseInt और parseFloat
parseInt() किसी स्ट्रिंग को शुरुआत से पढ़ता है और एक whole number निकालता है, पहले non-numeric character पर रुकते हुए, और parseFloat() भी वैसा ही करता है पर result में decimal point की इजाज़त देता है।
उदाहरण: parseInt and parseFloat
console.log(parseInt("42px")); // 42
console.log(parseFloat("3.14m")); // 3.14
आम Coercion Gotchas
JavaScript के कुछ coercion rules व्यापक रूप से हैरान करने वाले माने जाते हैं, जैसे Number('') का 0 return करना, और NaN का === से भी कभी खुद के बराबर न होना, इसीलिए इसे सही तरीके से पहचानने के लिए isNaN() या Number.isNaN() चाहिए।
उदाहरण: Common Coercion Gotchas
console.log(Number("")); // 0
console.log(NaN === NaN); // false
console.log(isNaN(NaN)); // true
console.log(Number.isNaN(NaN)); // true
असली Form Input को Convert करना
HTML form inputs से पढ़ी गई values, type=number fields सहित, JavaScript में हमेशा strings के रूप में आती हैं, जिससे user-submitted data पर कोई भी arithmetic करने से पहले explicit conversion एक ज़रूरी step बन जाता है।
उदाहरण: Converting Real Form Input
const inputValue = "25"; // value read from a form input, always a string
const age = Number(inputValue);
console.log(age + 5);
- यह भूल जाना कि HTML form inputs से आने वाली values हमेशा strings से शुरू होती हैं, भले ही वे numbers जैसी दिखें, पहले explicit conversion चाहिए।
- यह मान लेना कि Number('') NaN return करता है, यह असल में 0 return करता है, एक genuinely surprising conversion rule जो याद रखने लायक है।
- edge cases में radix argument के बिना parseInt इस्तेमाल करना, predictable base-10 parsing के लिए हमेशा explicitly 10 pass करें, parseInt(value, 10)।
- Implicit conversion automatically होता है, जैसे + किसी number और string को combine करता है, जबकि explicit conversion Number() जैसे functions से जानबूझकर किया जाता है।
- Number(), String(), और Boolean() किसी value को उस specific type में explicitly convert करते हैं।
- parseInt और parseFloat किसी string से एक leading number निकालते हैं, भले ही उसके बाद string में extra non-numeric characters हों।
Type conversion functions और coercion rules core JavaScript behaviors हैं, standardized और हर browser में identical।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic