Skip to content

Commit

Permalink
Merge branch 'defaultValues' of github.com:guidesmiths/js-pills into …
Browse files Browse the repository at this point in the history
…defaultValues
  • Loading branch information
Girgetto committed Nov 19, 2020
2 parents b8dcb33 + 1c00050 commit b9582ac
Showing 1 changed file with 48 additions and 49 deletions.
97 changes: 48 additions & 49 deletions basic/DefaultValues/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,93 @@ Here are four ways to set a default value for a variable in JavaScript, each one

This is useful when we want a default value to be overwritten only if a new value is set.

### :white_check_mark: Logical operator `||`
### Logical operator `||`
The first option is to set the default value with the logical operator `||`.

```js
let fruit;
fruit = fruit || 'apple';
console.log(fruit) // apple
```

```js
let fruit = 'strawberry';
fruit = fruit || 'apple';
console.log(fruit) // strawberry
const defaultLogical = fruit => {
fruit = fruit || 'apple';
console.log('fruit is '+ fruit)
}
defaultLogical() // fruit is apple
defaultLogical('strawberry') // fruit is strawberry
```



### :white_check_mark: Ternary operator
### Ternary operator
Another option we have is to set the default value by using the ternary operator.

```js
let fruit;
fruit = fruit ? fruit : 'apple';
console.log(fruit) // apple
```

```js
let fruit = 'strawberry';
fruit = fruit ? fruit : 'apple';
console.log(fruit) // strawberry
const defaultTernary = fruit => {
fruit = fruit ? fruit : 'apple';
console.log('fruit is '+ fruit)
}
defaultTernary() // fruit is apple
defaultTernary('strawberry') // fruit is strawberry
```



### :warning: Warning: falsy values
### ⚠️ Warning: falsy values

On the previous examples, if a falsy value like `''` or `0` is set on the variable, it will be overwritten by the default `apple`

```js
let fruit = '';
fruit = fruit || 'apple';
console.log(fruit) // apple
```
defaultLogical(0) // fruit is apple
defaultLogical('') // fruit is apple
defaultLogical(undefined) // fruit is apple

```js
let fruit = 0;
fruit = fruit ? fruit : 'apple';
console.log(fruit) // apple
defaultTernary(0) // fruit is apple
defaultTernary('') // fruit is apple
defaultTernary(undefined) // fruit is apple
```

If we want to avoid this behavior, and keep the falsy as possible values, we can use the default parameter
If we want to control this behavior, and keep the falsy as possible values, we can use the default parameter



### :white_check_mark: Default parameter
### Default parameter
With this method, if we set a default value for a parameter inside a function, we can avoid it from being overwritten in the case of passing falsy value, and maintain this falsy value as the final one.

So, depending on the behavior we expect when a falsy value comes in, we might want to use one of the options above or this one.


```js
const foo = (fruit = 'apple') => {
console.log(fruit)
const defaultWithParameter = (fruit = 'apple') => {
console.log('fruit is '+ fruit)
}

foo() // apple
foo(undefined) // apple
foo(0) // 0
foo('') // ''
foo('strawberry') // strawberry
defaultWithParameter(0) // fruit is 0
defaultWithParameter('') // fruit is ''
defaultWithParameter() // fruit is apple
defaultWithParameter(undefined) // fruit is apple
defaultWithParameter('strawberry') // fruit is strawberry
```



### :warning: Warning: `undefined`
Note that in all previous cases, if the value is set to `undefined`, it will be overwritten by the default one provided. To control this, we could apply an if/else statement.

### ⚠️ Warning: `undefined`
As you can see in all previous cases, if the value is set to `undefined`, it will be overwritten by the default one provided. To control this, we could apply an if/else statement.


### :white_check_mark: If/else
This option gives us more control over the code, but is also more verbose
### If/else
This option gives us more control over the code, but is also more verbose. It is the best option if we need to perform additional actions for each scenario.

```js
let fruit = undefined;

if (fruit) {
// do something
} else {
console.log(fruit + ' is still undefined')
const defaultIfElse = fruit => {
if (fruit) {
console.log('fruit is '+ fruit)
} else if (fruit === undefined) {
console.log('fruit is undefined')
} else if (fruit === '') {
console.log('fruit is an empty string')
} else if (fruit === 0) {
console.log('fruit is 0')
}
}
defaultIfElse('strawberry') // fruit is strawberry
defaultIfElse(undefined) // fruit is undefined
defaultIfElse('') // fruit is an empty string
defaultIfElse(0) // fruit is 0
```

0 comments on commit b9582ac

Please sign in to comment.