{"id":648,"date":"2024-07-30T19:44:58","date_gmt":"2024-07-30T17:44:58","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=648"},"modified":"2024-07-30T19:44:58","modified_gmt":"2024-07-30T17:44:58","slug":"007-undefined-null-nan","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/03-tipos-de-datos-en-javascript\/007-undefined-null-nan\/","title":{"rendered":"007. undefined, null, Nan"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong><em>null<\/em><\/strong>, <strong><em>undefined<\/em> <\/strong>y <strong><em>Nan<\/em> <\/strong>son casos particulares de <strong>Javascript<\/strong>, que a veces hay cierta confusi\u00f3n, sobre todo entre <strong><em>null<\/em> <\/strong>y <strong><em>undefined<\/em><\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">undefined<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Tanto <strong><em>null<\/em> <\/strong>como <strong><em>undefined<\/em> <\/strong>van a representar un valor ausente, es decir, una <strong>variable<\/strong> que no tiene valor, la gran diferencia es que mientras <strong><em>undefined<\/em> <\/strong>es una <strong>variable<\/strong> que no ha sido inicializada. Para que <strong>Javascript<\/strong> detecte que se trata de una <strong>variable indefinida<\/strong>, simplemente no asignamos ning\u00fan valor a dicha variable. <strong><em>undefined<\/em> <\/strong>es una <strong>variable<\/strong> que no tiene valor, y que aparte no se le asign\u00f3 ninguno a la hora a la que se cre\u00f3.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let indefinida;\nconsole.log(indefinida);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">null<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>null<\/em> <\/strong>es cuando creamos una <strong>variable<\/strong> y le asignamos intencionalmente el valor <strong><em>null<\/em><\/strong>, por lo tanto es un valor asignado por el programador, mientras que <strong><em>undefined<\/em> <\/strong>es un valor asignado por <strong>Javascript<\/strong> cuando detecta que una <strong>variable<\/strong> no tiene valor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Por lo tanto la gran diferencia entre <strong><em>null<\/em> <\/strong>y <strong><em>undefined<\/em> <\/strong>es que, ambos significan que la <strong>variable<\/strong> est\u00e1 vac\u00eda, pero <strong><em>undefined<\/em> <\/strong>significa que no ha sido asignado ning\u00fan valor, y <strong>Javascript<\/strong> internamente le ha asignado el valor <strong><em>undefined<\/em><\/strong>, mientras que <strong><em>null<\/em><\/strong>, la <strong>variable<\/strong> est\u00e1 vac\u00eda pero intencionalmente el programador le ha asignado ese valor especial de <strong><em>null<\/em><\/strong>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Nota<\/strong>: ambos valores significa que el valor est\u00e1 ausente, en <strong>undefined<\/strong> no se ha inicializado ning\u00fan valor, mientras que en <strong>null<\/strong> intencionalmente le hemos dado un valor <strong>null<\/strong>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">NaN<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Cuando queremos realizar operaciones aritm\u00e9ticas entre diferentes tipos de datos que no son num\u00e9ricos,&nbsp; <strong><em>NaN<\/em> <\/strong>significa <strong>not a number<\/strong>, y la <strong>consola Javascript<\/strong> nos arrojar\u00e1 un <strong><em>NaN<\/em> <\/strong>cuando asignemos o hagamos operaciones donde alguno de los datos en cuesti\u00f3n no sea un n\u00famero.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let noEsUnNumero = \"Hola\" * 3.7;\nconsole.log(noEsUnNumero); \/\/ IMPRIMIR\u00c1 NaN<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>null, undefined y Nan son casos particulares de Javascript, que a veces hay cierta confusi\u00f3n, sobre todo entre null y undefined.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":628,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","footnotes":""},"class_list":["post-648","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"null, undefined y Nan son casos particulares de Javascript, que a veces hay cierta confusi\u00f3n, sobre todo entre null y undefined.","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=648"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/648\/revisions"}],"predecessor-version":[{"id":650,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/648\/revisions\/650"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/628"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}