{"id":136265,"date":"2024-01-30T17:24:40","date_gmt":"2024-01-30T17:24:40","guid":{"rendered":"https:\/\/www.sushilkumar.ind.in\/blog\/?p=136265"},"modified":"2024-01-31T12:57:56","modified_gmt":"2024-01-31T12:57:56","slug":"what-are-truthy-and-falsy-values-in-javascript","status":"publish","type":"post","link":"https:\/\/www.sushilkumar.ind.in\/blog\/javascript\/what-are-truthy-and-falsy-values-in-javascript\/","title":{"rendered":"What are truthy and falsy values in JavaScript?"},"content":{"rendered":"\n<p>In JavaScript, a value is considered &#8220;<strong>truthy<\/strong>&#8221; if it evaluates to true when evaluated in a boolean context. Conversely, a value is considered &#8220;<strong>falsy<\/strong>&#8221; if it evaluates to false when evaluated in a boolean context.<\/p>\n\n\n\n<p>Here&#8217;s a breakdown of truthy and falsy values in JavaScript:<\/p>\n\n\n\n<p><strong>Truthy values:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Non-empty strings: Any string with at least one character.<\/li>\n\n\n\n<li>Non-zero numbers: Any number except 0, including negative numbers and decimals.<\/li>\n\n\n\n<li>true: The boolean value true.<\/li>\n\n\n\n<li>Objects: Any non-null object or array, including empty objects and arrays.<\/li>\n\n\n\n<li>Functions: Any non-null function.<\/li>\n<\/ol>\n\n\n\n<p><strong>Falsy values:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Empty strings: A string with no characters, represented as <code>''<\/code> or <code>\"\"<\/code>.<\/li>\n\n\n\n<li>0: The number zero.<\/li>\n\n\n\n<li>NaN: Not-a-Number, a special value resulting from invalid operations.<\/li>\n\n\n\n<li>false: The boolean value false.<\/li>\n\n\n\n<li>null: The absence of any value or object.<\/li>\n\n\n\n<li>undefined: A variable that has not been assigned a value, or a function with no return value.<\/li>\n\n\n\n<li>Document.all: A legacy property that evaluates to false in modern browsers.<\/li>\n<\/ol>\n\n\n\n<p>Here&#8217;s a simple illustration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if ('hello') {\n    console.log('Truthy'); \/\/ This will be printed\n}\n\nif (0) {\n    console.log('Falsy');\n} else {\n    console.log('Falsy'); \/\/ This will be printed\n}\n<\/code><\/pre>\n\n\n\n<p>Understanding truthy and falsy values is important in JavaScript, especially when working with conditional statements or type coercion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, a value is considered &#8220;truthy&#8221; if it evaluates to true when evaluated in a boolean context. Conversely, a value is considered &#8220;falsy&#8221; if it evaluates to false when evaluated in a boolean context. Here&#8217;s a breakdown of truthy and falsy values in JavaScript: Truthy values: Non-empty strings: Any string with at least one &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[323],"tags":[],"class_list":["post-136265","post","type-post","status-publish","format-standard","","category-javascript"],"jetpack_publicize_connections":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p99pkJ-zrP","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136265"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=136265"}],"version-history":[{"count":1,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136265\/revisions"}],"predecessor-version":[{"id":136266,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136265\/revisions\/136266"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=136265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/categories?post=136265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/tags?post=136265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}