Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

3/01/2013

Fun with type casting in javascript

Let's have some fun with type casting in javascript today :

Do you know what is:

"2" // string as we know


what about:

+"2" // number


but wait, what is this:

1 + +"2" //  3
1 - - "3" // 4


boolean:

+true // 1
+false // 0
-true // -1
-false // 0
!!true // true
!!false // false


meet miss tilde:

~true // -2
~~true // 1
~false // -1
~~false // 0
1 - ~true // 3
1 + ~~false // 1


a real fun:

- ~ -2 // -1
- ~false //1
- ~0 // 1
! ~ -2 // false
! ~ -1 //true



You just saw hidden treasure of our favourite language.

Happy Coding :)


No comments:

Post a Comment