Learn javaScript type conversion


JavaScript variables can be converted to a new variable:


Converting Javascript numbers to strings


Use the String( ) global method to convert numbers to string:


Code Example:

                    String(234)       // returns a string 
                   String(d)         // returns a string from a number variable d
String(10 + 5)  
                 
                 
Try yourself >>






Converting Javascript booleans to strings


Use the String( ) global method to convert booleans to strings:


Code Example:

                    String(false)      // returns "false"
String(true)       // returns "true"
                 
                 





Converting Javascript dates to strings


Use the String( ) global method to convert dates to strings:


Code Example:

                    String(Date())  // returns "Thu month day year 12:12:12 GMT+numberarea (area day time)"

                 
                 


More methods that you can use:


Method Description
getDay() weekday (0-6)
getDate() day as a number (1-31)
getHours() hour (0-23)
getFullYear() four digit year (yyyy)
getMonth() month (0-11)
getTime() time (milliseconds since January 1, 1970)
getMinutes() minutes (0-59)
getSeconds() seconds (0-59)
getMilliseconds() milliseconds (0-999)




Converting Javascript strings to numbers


Use the Number( ) global method to convert strings to numbers:


Code Example:

                    Number(" ")       // returns 0
Number("5.11")    // returns 5.11
Number("")        // returns 0
Number("22 33")   // returns NaN
                 
                 



The Unary + Operator


Use the unary + operator to convert a variable to a number:


Code Example:

                    var a = "10";      // a is a string
var b = + a;      // b is a number
                 
                 
Try yourself >>





Converting booleans to numbers


Use the Number( ) to convert booleans to numbers:


Code Example:

      Number(false)     // returns 0
Number(true)      // returns 1
   
   





Converting dates to numbers


Use the Number( ) to convert dates to numbers:


Code Example:

      d = new Date();
Number(d)          // returns 1404568027739
   
   

The date method getTime() does the same.






JavaScript Type Conversion Table


some results of diffrenet converting actions

Original Value Converted to Number Converted to String Converted to Boolean
0 0 "0" false
1 1 "1" true
true 1 "true" true
false 0 "false" false
"000" 0 "000" true
"ten" NaN "ten" true
" " 0 " " false
NaN NaN "NaN" false
function(){} NaN "function(){}" true
undefined NaN "undefined" false
null 0 "null" false
[ ] 0 " " true



JavaScript Data Types


there are 5 data types in Javascript:

  • number
  • string
  • boolean
  • function
  • object



the next data types can't contain values:

  • undefined
  • null



In Javascript there are 6 types of objects:


  • Date
  • String
  • Object
  • Array
  • Boolean
  • Number



String() converts to a String


Number() converts to a Number


Boolean() converts to a Boolean





Javascript typeof operator


to find data type of a variable use the typeof operator:



Code Example:

                    typeof 5.11                   // Returns "number"
                   typeof "Jake"                 // Returns "string"
                   typeof true                  // Returns "boolean"
typeof [2,3,4,5]              // Returns "object"
typeof {name:'Jake', age:12}  // Returns "object"
typeof NaN                    // Returns "number"
typeof myHat                  // Returns "undefined" *
typeof function () {}         // Returns "function"
typeof null                   // Returns "object"
typeof new Comment()             // Returns "object"
                 
                 
Try yourself >>




Javascript constructor property


to find the a variable constructor function we use the constructor property:



Code Example:

                    (5.11).constructor                // Returns function Number()  {[native code]}
"Jake".constructor                // Returns function String()  {[native code]}
true.constructor                 // Returns function Boolean() {[native code]}
function () {}.constructor        // Returns function Function(){[native code]}
                 
                 
Try yourself >>


If you want to check if the object is an Array function:



Code Example:

                    function isArray(myArray) {
  return myArray.constructor === Array;
}
                 
                 
Try yourself >>








we hope you enjoyed this lesson, learn javascript from scratch, we create many free online javascript course, we add programming courses on a daily basis, learn javascript for web development



Navigation