JavaScript Basic Type Conversions Cheat Sheet 🔥
Tags: webdev, javascript, beginners, cheatsheet
Published: 2021-04-07 17:35:52

## Table Of Contents
   * [String conversion](#string-conversion)
   * [Boolean conversion](#boolean-conversion)
   * [Number conversion](#number-conversion)
   * [Null conversion](#null-conversion)
   * [Undefined conversion](#undefined-conversion)

**Type conversion can be explicit or implicit.**
```javascript
value = Number('23') // explicit
value = 5 + '25' // implicit
```
**Value type checking**
```javascript
console.log(typeof value);
```

# String conversion <a name="string-conversion"></a>
#### Number to string:
```javascript
value = String(10); /* => '10' */
value = String(10 + 40); /* => '50' */
value = (10 + 40).toString(); /* => '50' */
value = new String(10 + 20); /* => '30' */
```
#### Boolean to string
```javascript
value = String(true); /* => 'true' */
value = String(false); /* => 'false' */
```
#### Array to string
```javascript
value = String([1, 2, 3]); /* => '1,2,3' */
value = String([ ]); /* => '' */ 
```
#### Object to string 
```javascript
value = String({name: "Daniel"}); /* => [object Object] */
```
Conversion to string occurs when any data type is concatenated with a string (implicit conversion):
```javascript
value = 30 + ' ' + 30; /* => 30 30 */ // Space is considered a symbol.
value = 30 + '' + undefined; /* => 30undefined */
```
Mathematical operations convert Empty String to zero:
```javascript
value = 30 - ''; /* => 30 */ 
value = 30 - 'text'; /* => NaN */ // If the string is not empty, then we will get NaN - calculation error.
value = 30 - '5'; /* => 25 */ // If we write a number in a string, we will get a number type
```
---

# Boolean type conversion <a name="boolean-conversion"></a>
In mathematical operations, true is converted to one and false to zero:
```javascript
value = true + 5; /* => 6 */
value = false + 5; /* => 5 */
```
#### String to boolean
```javascript
value = Boolean('hello'); /* => true */ // Any non-empty string will be considered true.
value = Boolean(' '); /* => true */ 
value = Boolean(''); /* => false */ // An empty string will be considered false.
```
#### Number to boolean
```javascript
value = Boolean(-123); /* => true */ // Any number, both positive and negative, will be considered true.
value = Boolean(123); /* => true */
value = Boolean(0); /* => false */ // Zero counts as false
```
#### Undefined to boolean
```javascript
value = Boolean(undefined); /* => false */
```
#### Null to boolean
```javascript
value = Boolean(null); /* => false */
```
#### Object to boolean
```javascript
value = Boolean({}); /* => true */ // An empty object is considered true.
```
#### Array to boolean
```javascript
value = Boolean([]); /* => true */ // An empty array is considered true.
```
---

# Number type conversion <a name="number-conversion"></a>
#### String to number
```javascript
value = Number('23'); /* => 23 */
value = Number('string...lalala'); /* => NaN */ 
value = parseInt(' 203px'); /* => 203 */ // The parseInt function reads a number from a string and removes all characters after it, but if there are characters before the number (except for a space), then it will output NaN. Serves for whole numbers.
value = parseFloat('203.212px'); /* => 203.212 */ // Works the same as parseInt, but for fractional numbers.
```
#### Boolean to number
```javascript
value = Number(true); /* => 1 */
value = Number(false); /* => 0 */
```
#### Null to number
```javascript
value = Number(null); /* => 0 */
```
#### Array to number 
```javascript
value = Number([1, 2, 3]); /* => NaN */ // NaN refers to numbers.
```
---

# Null type conversion <a name="null-conversion"></a>
Converts to zero for mathematical operations:
```javascript
value = null + 5; /* => 5 */
```
---

# Undefined type conversion <a name="undefined-conversion"></a>
Converts to NaN for mathematical operations:
```javascript
value = false + undefined; /* => NaN */
```
---
Thank's for reading! ❤️
You can also check out my [redux basics cheat sheet](https://dev.to/danielkrupnyy/redux-basics-cheat-sheet-1kdn)