Creative Developals

Creative Developals

Teilen

Photos from Creative Developals's post 14/01/2022

Do you know what happens when you type a website on your browser and press enter ?

29/04/2021

Table of Content

1. What are Truthy and Falsy values in JS?
2. Double negation
3. Double Or
4. Double question mark

1.What are Truthy and Falsy values in JS?

Truthy values: In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context.
reference: https://developer.mozilla.org/en-US/docs/Glossary/Truthy

Falsy values: A falsy (sometimes written falsely) value is a value that is considered false when encountered in a Boolean context.
reference: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
--------------------------------------------------------------

2.Double negation: this symbol does a double evaluation.

The way this works, it evaluates if the value is falsy and if it is, it returns false otherwise true. The first "!" close to the value evaluates to true or false, and the second "!" negates the result returned by the first "!" value.

Usecase: JavaScript and TypeScript

Code sample:

/***
* !! Code below
*: JS & TS
*/

const emptyStr = '';
const nullish = null;
const undefine = undefined;
const zero = 0;

if(!!emptyStr && !!nullish && !!undefine && !!zero) console.log("You are all truthy values");

else console.log("You are all falsy value");
--------------------------------------------------------------

3.Double Or: this returns the right-hand side when the left-hand side of the expression evaluates to a falsy value. it's like an if-else statement

Usecase: JavaScript and TypeScript

Code sample:

/***
* || Code below
*: JS & TS
*/

console.log(`You are a ${emptyStr || "fase"}ly value`);
--------------------------------------------------------------

4.Double question: this is used as a fallback option if the left-hand side is undefined or null. Note this only covers undefined and null values in TypeScript and the "Double Or || operand" covers it because undefined or null results in a falsy value.

Usecase: TypeScript only

Code Sample:

/***
* ?? Code below
*: TS only
*/

console.log(`You are a ${emptyStr ?? "fase"}ly value`);//anti-usage

console.log(`You are a ${undefine ?? "fase"}ly value`);//good usage

Wollen Sie Ihr Service zum Top-Computer- Und Elektronikservice in Munich machen?
Klicken Sie hier, um Ihren Gesponserten Eintrag zu erhalten.

Kategorie

Webseite

Adresse


Munich
80863