From d6751b5fdb67a11da60def40660b533a693e7790 Mon Sep 17 00:00:00 2001 From: Yash Handa Date: Fri, 15 Feb 2019 01:47:43 +0530 Subject: [PATCH] Update what-are-truthy-and-falsy-values.md --- .../what-are-truthy-and-falsy-values.md | 74 ++++++++++--------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/locale/en/knowledge/javascript-conventions/what-are-truthy-and-falsy-values.md b/locale/en/knowledge/javascript-conventions/what-are-truthy-and-falsy-values.md index 70af00eb00fcf..995c6eeea4c86 100644 --- a/locale/en/knowledge/javascript-conventions/what-are-truthy-and-falsy-values.md +++ b/locale/en/knowledge/javascript-conventions/what-are-truthy-and-falsy-values.md @@ -5,7 +5,7 @@ tags: - falsy - types - coercion -title: 'What are "truthy" and "falsy" values?' +title: 'What are "truthy" and "falsy" values?' difficulty: 4 layout: knowledge-post.hbs --- @@ -15,60 +15,68 @@ JavaScript is weakly typed language. That means different types can be used in operations and the language will try to convert the types until the operation makes sense. - console.log("1" > 0); // true, "1" converted to number - console.log(1 + "1"); // 11, 1 converted to string +```js +console.log("1" > 0); // true, "1" converted to number +console.log(1 + "1"); // 11, 1 converted to string +``` -Type conversion also applys when values are used in unary boolean +Type conversion also applies when values are used in unary boolean operations, most notably if statements. If a value converts to the boolean true, then it is said to be "truthy". If it converts to false it is "falsy". - var myval = "value"; - if(myval) { - console.log("This value is truthy"); - } +```js +let myval = "value"; +if(myval) { + console.log("This value is truthy"); +} - myval = 0; - if(!myval) { - console.log("This value is falsy"); - } +myval = 0; +if(!myval) { + console.log("This value is falsy"); +} +``` Since most values in javascript are truthy, e.g. objects, arrays, most numbers and strings, it's easier to identify all of the falsy values. These are: - false // obviously - 0 // The only falsy number - "" // the empty string - null - undefined - NaN +```js +false // obviously +0 // The only falsy number +"" // the empty string +null +undefined +NaN +``` Note that all objects and arrays are truthy, even empty ones. Truthiness and Falsiness also come into play with logical operators. When using logical AND/OR, the values will be converted -based on truthiness or falsyness and then the expression will resolve +based on truthiness or falseness and then the expression will resolve to the last truthy value. Short circuit rules apply. Here's an extended example. - var first = "truthy" - , second = "also truthy"; +```js +let first = "truthy", + second = "also truthy"; - var myvalue = first && second; - console.log(myvalue); // "also truthy" +let myvalue = first && second; +console.log(myvalue); // "also truthy" - first = null; - second = "truthy"; +first = null; +second = "truthy"; - myvalue = first || second; - console.log(myvalue); // "truthy" +myvalue = first || second; +console.log(myvalue); // "truthy" - myvalue2 = second || first; - console.log(myvalue2); // "truthy" +myvalue2 = second || first; +console.log(myvalue2); // "truthy" - var truthy = "truthy" - , falsy = 0; +let truthy = "truthy", + falsy = 0; - myvalue = truthy ? true : false; - myvalue = falsy ? true : false; +myvalue = truthy ? true : false; +myvalue = falsy ? true : false; +```