Created
November 27, 2014 05:41
-
-
Save ui2code/b201a655095a6f82a24d to your computer and use it in GitHub Desktop.
Operadores
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ~, !, +, & - | |
// http://dreaminginjavascript.wordpress.com/2008/07/04/28/ | |
//Cara o "~" é um operator lógico (bitwise) NÃO (not). Ele tem um algorítimo simples que é o -(n+1). | |
//O que foi usado nesse caso, foi apenas para retornar true ou false. É um uso bem legal! | |
//Outra coisa legal também é que o "double tilde" (~~) é bem usado para substituir o Math.floor(), por ser menor, e em algumas situações, mais rápido! | |
console.log(~-2); //1 | |
console.log(~-1); //0 | |
console.log(~0); //-1 | |
console.log(~1); //-2 | |
console.log(~2); //-3 | |
console.log(~true); //-2 | |
console.log(~false); //-1 | |
var str="teste"; | |
if( ~str.indexOf('testando') ){ | |
console.log('if'); | |
} else { | |
console.log('else'); | |
} | |
var a = Math.floor( 45.95); | |
console.log( a ); | |
var b = ~~45.95; | |
console.log( b ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment