DEV Community

Mubasshir Ahmed
Mubasshir Ahmed

Posted on

Data Type and Type Casting

DailyJS

There are 5 different data types that contain values.
1) String
2) Number
3) Boolean
4) Object
5) Function
There are 6 types of objects.
1) Object
2) Date
3) Array
4) String
5) Boolean
There are two types that don't contain values.
1) NUll 2) Undefined

Type Casting

There are two types of typecasting.
1) implicit conversion.
2) Explicit conversion.

implicit conversion :

In certain situations, javascript automatically converts one data type to another.

var res ;
res = '3' + 5 //output '35'
res = '3' + true  //output : '3true'
res = '3' + null //output  : '3null'
res = null + '3' //output  : 'null3'
res = null + null //output : 0


res = '10' - '3'  // output : 7 
res = '10' - 3    // output : 7
res = '10' * 3    // output : 30
res = '10' / 2    // output : 5

res = 'hello' - 5  // output : NaN

res = '7' - true   // output : 6 
res = '7' + true   // output : 8
Enter fullscreen mode Exit fullscreen mode

Explicit Conversion

In Explicit one data type convert to another manually.

// string to number 
res = Number('324')  // 324
res = parseInt('20.01')  // 20
res = parseFloat('20.01') // 20.01

//convert to string
res = String(324) 


Enter fullscreen mode Exit fullscreen mode

Top comments (0)