In JavaScript, type conversion is the process of changing the data type of a variable or a value. This can be done using various built-in functions and methods.
Few Examples of Type ConversionType conversion Methods
JavaScript also has some unary operators that perform type conversion
It is also possible to convert a value to a different type using the valueOf() and toString() methods.
JavaScript also has some automatic type coercion which happens when different types are being used together in an operation. For instance, if a string is added to a number, JavaScript will convert the string to a number before performing the addition.
It's important to keep in mind that type conversion can lead to unexpected results if not handled properly.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tutor Joes</title> </head> <body> <script src="js/script.js"></script> </body> </html>To download raw file Click Here
//Type Conversion let a; //Others to String a=25; console.log(a,typeof a); a=String(25); console.log(a,typeof a); a=25.5; console.log(a,typeof a); a=String(25.5); console.log(a,typeof a); a=true; console.log(a,typeof a); a=String(true); console.log(a,typeof a); a=new Date(); console.log(a,typeof a); a=String(a); console.log(a,typeof a); a=[1,2,3,4,5] console.log(a,typeof a); a=String(a); console.log(a,typeof a); a=25 console.log(a,typeof a); a=a.toString(); console.log(a,typeof a); //String to number a="1234" console.log(a,typeof a); a=Number(a); console.log(a,typeof a); a=true; console.log(a,typeof a); a=Number(a); console.log(a,typeof a); a=[1,2,3,4,5]; console.log(a,typeof a); a=Number(a); console.log(a,typeof a); a="Tutor Joes"; console.log(a,typeof a); a=Number(a); console.log(a,typeof a); a='35'; console.log(a,typeof a); a=parseInt(a); console.log(a,typeof a); a='35.55'; console.log(a,typeof a); a=parseInt(a); console.log(a,typeof a); a='35.55'; console.log(a,typeof a); a=parseFloat(a); console.log(a,typeof a);To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions