03.11.2014 Views

Introduccion a AJAX - Web de jEsuSdA 8

Introduccion a AJAX - Web de jEsuSdA 8

Introduccion a AJAX - Web de jEsuSdA 8

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Introducción a <strong>AJAX</strong><br />

Capítulo 2. JavaScript básico<br />

2.4.1.4. Variables <strong>de</strong> tipo boolean<br />

A<strong>de</strong>más <strong>de</strong> variables <strong>de</strong> tipo boolean, también se suelen llamar variables lógicas y variables<br />

booleanas. Se trata <strong>de</strong> una variable que sólo pue<strong>de</strong> almacenar uno <strong>de</strong> los dos valores especiales<br />

<strong>de</strong>finidos y que representan el valor "verda<strong>de</strong>ro" y el valor "falso".<br />

var variable1 = true;<br />

var variable2 = false;<br />

Los valores true y false son valores especiales, <strong>de</strong> forma que no son palabras ni números ni<br />

ningún otro tipo <strong>de</strong> valor. Este tipo <strong>de</strong> variables son esenciales para crear cualquier aplicación,<br />

tal y como se verá más a<strong>de</strong>lante.<br />

Cuando es necesario convertir una variable numérica a una variable <strong>de</strong> tipo boolean, JavaScript<br />

aplica la siguiente conversión: el número 0 se convierte en false y cualquier otro número<br />

distinto <strong>de</strong> 0 se convierte en true.<br />

Por este motivo, en ocasiones se asocia el número 0 con el valor false y el número 1 con el valor<br />

true. Sin embargo, es necesario insistir en que true y false son valores especiales que no se<br />

correspon<strong>de</strong>n ni con números ni con ningún otro tipo <strong>de</strong> dato.<br />

2.4.1.5. Variables <strong>de</strong> tipo numérico<br />

Las variables numéricas son muy utilizadas en las aplicaciones habituales, ya que permiten<br />

almacenar cualquier valor numérico. Si el número es entero, se indica directamente. Si el<br />

número es <strong>de</strong>cimal, se <strong>de</strong>be utilizar el punto (.) para separar la parte entera <strong>de</strong> la <strong>de</strong>cimal.<br />

var variable1 = 10;<br />

var variable2 = 3.14159265;<br />

A<strong>de</strong>más <strong>de</strong>l sistema numérico <strong>de</strong>cimal, también se pue<strong>de</strong>n indicar valores en el sistema octal (si<br />

se incluye un cero <strong>de</strong>lante <strong>de</strong>l número) y en sistema hexa<strong>de</strong>cimal (si se incluye un cero y una x<br />

<strong>de</strong>lante <strong>de</strong>l número).<br />

var variable1 = 10;<br />

var variable_octal = 034;<br />

var variable_hexa<strong>de</strong>cimal = 0xA3;<br />

JavaScript <strong>de</strong>fine tres valores especiales muy útiles cuando se trabaja con números. En primer<br />

lugar se <strong>de</strong>finen los valores Infinity y –Infinity para representar números <strong>de</strong>masiado<br />

gran<strong>de</strong>s (positivos y negativos) y con los que JavaScript no pue<strong>de</strong> trabajar.<br />

var variable1 = 3, variable2 = 0;<br />

alert(variable1/variable2); // muestra "Infinity"<br />

El otro valor especial <strong>de</strong>finido por JavaScript es NaN, que es el acrónimo <strong>de</strong> "Not a Number". De<br />

esta forma, si se realizan operaciones matemáticas con variables no numéricas, el resultado será<br />

<strong>de</strong> tipo NaN.<br />

Para manejar los valores NaN, se utiliza la función relacionada isNaN(), que <strong>de</strong>vuelve true si el<br />

parámetro que se le pasa no es un número:<br />

www.librosweb.es 14

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!