Created
February 24, 2012 13:04
-
-
Save herberthamaral/1900797 to your computer and use it in GitHub Desktop.
wtf.js
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
console.log(999999999999999) | |
//999999999999999 | |
console.log(9999999999999999) | |
//10000000000000000 ==> WTF?? | |
console.log(9999999999999999+1) | |
//10000000000000000 | |
console.log(9999999999999999+2) | |
//10000000000000002 ==> WTF^2?? | |
console.log(false || 'http') | |
//http | |
console.log(true || 'http') | |
//true | |
O estranho do short-circuit é que ele se comporta de forma diferente mesmo com variáveis do mesmo "tipo" (bool).
É um dos perigos de se usar short-circuit em js, pq é a característica de truthy/falsy do valor que é considerada.
// suponha que uma string vazia seja um valor válido como parâmetro
function func(param) {
param = param || 'noop';
console.log(param);
}
func(''); // 'noop'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nos exemplos 2 a 4 parece ser overflow, mas realmente não faz sentido mesmo considerando que o maior número possível em js é 2^53.
A explicação dos 2 últimos exemplos é short-circuit evaluation; é bastante usado com optional parameters/settings.