Share your knowledge with other learners . . . Login Register

JavaScript Type Conversion In Hindi

📔 : Java Script 🔗

Type Conversion एक mechanism / process है जिसमे हम किसी दी गयी value को दुसरे type में convert करते हैं। इसे Type Casting भी कहते हैं।


हम जानते हैं कि JavaScript dynamic type language है means किसी variable को initialize करते समय हमें इसमें assign की गयी value के type को declare नहीं करना पड़ता है , जिस भी तरह की हम value assign करते है JavaScript Engine इसे automatically convert कर देता है।

JS String Type Conversion

किसी दिए गए number या boolean value , string में convert करने के लिए String() function का use करते हैं।

Another File : string_conversion.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html >
<html>
  <head>
    <meta charset="utf-8">
    <title>Js String Type Conversion</title>
  </head>
  <body>
    <script>
      let x  = 12; /* number to string*/
      document.write('Now type of x : '+ typeof String(x));
      let y  = true; /* boolean to string*/
      document.write('<br>Now type of y : '+ typeof String(y));
      let z  = null; /* null to string*/
      document.write('<br>Now type of z : '+ typeof String(z));
    </script>
  </body>
</html>
Output
Now type of x : string
Now type of y : string
Now type of z : string

JS Number Type Conversion

JavaScript में Number type conversion के लिए Number() function का use करते हैं।

Another File : string_conversion.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html >
<html>
  <head>
    <meta charset="utf-8">
    <title>Js Number Type Conversion</title>
  </head>
  <body>
    <script>
      let x  = '12'; /*single quoted string to number*/
      document.write('Now type of x : '+ typeof Number(x));
      let y  = "10"; /*double quoted string to number*/
      document.write('<br>Now type of y : '+ typeof Number(y));
      let z  = null; /*null to Number*/
      document.write('<br>Now type of z : '+ typeof Number(z));
    </script>
  </body>
</html>
Output
Now type of x : number
Now type of y : number
Now type of z : number

JS Boolean Type Conversion

किसी दी गई number या string value , boolean में number करने के लिए Boolean() function का use करते हैं।


हालाँकि JavaScript में empty string , 0 , false , null , undefined, empty array हमेशा false ही होते हैं , इसके अलावा string (except empty string) , object , array (that have some value), defined variable (जिसकी value empty string , 0 , false , null नहीं है) हमेशा true होते हैं।

Another File : bool_conversion.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html >
<html>
  <head>
    <meta charset="utf-8">
    <title>Js Boolean Type Conversion</title>
  </head>
  <body>
    <script>
      document.write('type of "true" : '+ Boolean("True"));
      document.write('<br>string converts into true : '+Boolean('mystring'));
      document.write('<br>string that contain 0 converts into true : '+Boolean('0'));
      document.write('<br>empty string converts into false: '+Boolean(''));
      document.write('<br>null converts into false : '+Boolean(null));
      document.write('<br>numeric value 0 converts into false : '+Boolean(0));
      let x;
      document.write('<br>undefined variable converts into false : '+Boolean(x));
    </script>
  </body>
</html>
Output
type of "true" : true
string converts into true : true
string that contain 0 converts into true : true
empty string converts into false: false
null converts into false : false
numeric value 0 converts into false : false
undefined variable converts into false : false

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers