[Cheat Sheet] Javascript – Type Conversion

Note: This is part of Javascript learning roadmap.

Object wrappers play an important role in type conversion as well in Javascript. It’s important to know the situations where an implicit type coercion is made and what to expect from it.


From other data types into number

  • Number() – for all data types
  • +some_value – (unary operator) same as Number()
  • isNumber()
    • only for string -> number
    • global function
    • returns the first valid number from the input and ignores the rest
    • if the input doesn’t start with a valid number, returns NaN
  • isFloat()
    • similar to isNumber()

Examples:

var a1 = 'YO',
    a2 = '',
    a3 = '232',
    a4 = '23243blue',
    a5 = false,
    a6 = true,
    a7 = undefined,
    a8 = null;
console.log(Number(a1)); // NaN
console.log(Number(a2)); // 0 !!! 
console.log(Number(a3)); // 232
console.log(Number(a4)); // NaN
console.log(Number(a5)); // 0
console.log(Number(a6)); // 1
console.log(Number(a7)); // NaN
console.log(Number(a8)); // 0

console.log(parseInt(a1)); // NaN
console.log(parseInt(a2)); // NaN !!! 
console.log(parseInt(a3)); // 232
console.log(parseInt(a4)); // 23243 !!!!
console.log(parseInt(a5)); // NaN
console.log(parseInt(a6)); // NaN
console.log(parseInt(a7)); // NaN
console.log(parseInt(a8)); // NaN

Check for NaN

  • isNaN() function first converts to value to Number() and then check for NaN – ex. isNaN(‘ex’) == true
  • if you don’t know if the value you are checking is a number or not, the best way to check if a value is NaN  is to check the equality with itself (only NaN != NaN)
  • an utility function which checks if a value is NaN, not if Number(value) is NaN would be:
isReallyNaN(v) {
    return v !== v;
}

From other data types into boolean

  • Boolean(value) – very important because conditional statements (like if) use this conversion when evaluating an expression
  • falsy values – values that converted via Boolean(value) are equal to false (don’t pass an if statement)
    • false
    • 0 (zero)
    • “” (empty string)
    • null
    • undefined
    • NaN (a special Number value meaning Not-a-Number)

From other data type to String

  • toString() 
    • method available for every data type (except undefined and null)
    • has on optional parameter – radix – used with numbers converts to value into the desired radix (ex var a = 10;   a.toString(16);   // equal to ‘a’ ) 
  • String()
    • if value has a toString() method -> call the method and return result
    • for undefined -> return ‘undefined’
    • for null -> return ‘null’

Type detection

  • typeof – problem when type is function on some browsers
  • instanceof  – problem when object is in a different global scope (different frame)
  • Object.prototype.toString.call(my_obj) (check if equal to ‘[Object Function]’, [Object Array], etc)
    • most consistent

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s