DEV Community

Ranjith srt
Ranjith srt

Posted on • Edited on

Js | Type Conversion & Type Coercion |

Type Conversion (Manually)
Enter fullscreen mode Exit fullscreen mode

Type conversion (also known as type casting) is when you explicitly convert a
value from one type to another. JavaScript provides several functions for this purpose.

Type Conversion:
Enter fullscreen mode Exit fullscreen mode
String to Number:
Enter fullscreen mode Exit fullscreen mode

let strNum ="123" ; //str
let num = Number(strNum);
   123  =  123    "123"


console.log(num); //123
console.log(typeof num); //number

Enter fullscreen mode Exit fullscreen mode
Number to String:
Enter fullscreen mode Exit fullscreen mode

let num=456;
let str =String(num);
         "456"   456


console.log(num); //"459"
console.log(typeof num); //string

Enter fullscreen mode Exit fullscreen mode
Boolean to String:
Enter fullscreen mode Exit fullscreen mode

let bool = true;
let strbool =String(bool);
              "true" true


console.log(strbool); //"true"
console.log(typeof num); //string

Enter fullscreen mode Exit fullscreen mode
String to Boolean:
Enter fullscreen mode Exit fullscreen mode

let strtrue="true"; 
let strfalse="false"; //true

//let strfalse=" ";  //false  //truthy fasleshy


let booltrue=Boolean(strtrue);
let boolfalse=Boolean(strfalse);


console.log(booltrue); //true
console.log(boolfalse);  //true

Enter fullscreen mode Exit fullscreen mode
Parsing integers and floats:
Enter fullscreen mode Exit fullscreen mode

let floatstr = "123.456";
let intnum = parseint(floatstr);
             integer   123.456

Enter fullscreen mode Exit fullscreen mode

let floatnum = parsefloat(floatstr);
                 123.456            123.456

console.log(floatnum); // no lose apdiyea print agum (flost)

output:
123.46

console.log(intnum); //456 lose last numbers

output;
123

Enter fullscreen mode Exit fullscreen mode
Type Coercion: 
Enter fullscreen mode Exit fullscreen mode

Type coercion is when JavaScript automatically converts a
value from one type to another during an operation.
This often happens with equality checks and arithmetic operations.

Type Coercion (Automatically) 
Enter fullscreen mode Exit fullscreen mode
String and Number:
Enter fullscreen mode Exit fullscreen mode

let result result ="5"+2;  // (+) jscript str cconcadinate aga + ah eduthukkum.
console.log(result);

output:
   52

Enter fullscreen mode Exit fullscreen mode

let result result ="5"-2;  // convert to mathematic function automatically.. 
console.log(result);


 output:
 3

Enter fullscreen mode Exit fullscreen mode

let result result ="5"*2;  // convert to mathematic function automatically.. 
console.log(result);


 output:
 10

Enter fullscreen mode Exit fullscreen mode
Boolean and Number:
Enter fullscreen mode Exit fullscreen mode

             1  +  1
let result =true + 1;  
console.log(result);


 output:
 2

Enter fullscreen mode Exit fullscreen mode
             0  +  1
let result =false + 1;  
console.log(result);


 output:
 1
Enter fullscreen mode Exit fullscreen mode
Coercion occurs in equality checks (==), but not in strict equality checks (===).
Enter fullscreen mode Exit fullscreen mode
Equality checks
Enter fullscreen mode Exit fullscreen mode
console.log(5=="5");  true // "5" vaa == automatic aa numberku convert panni irukku..
Enter fullscreen mode Exit fullscreen mode
console.log(5==="5");  false // data type value equval ah check pannum..
Enter fullscreen mode Exit fullscreen mode
console.log(5==5); true   
Enter fullscreen mode Exit fullscreen mode
                  0
console.log(0==false);   //true
Enter fullscreen mode Exit fullscreen mode
console.log(0===false);  //false
Enter fullscreen mode Exit fullscreen mode

Top comments (0)