JavaScript Basic Type Conversions Cheat Sheet ?

Table Of Contents

String conversion
Boolean conversion
Number conversion
Null conversion
Undefined conversion

Type conversion can be explicit or implicit.

value = Number(’23’) // explicit
value = 5 + ’25’ // implicit

Value type c…


This content originally appeared on DEV Community and was authored by Daniel Krupnyy

Table Of Contents

Type conversion can be explicit or implicit.

value = Number('23') // explicit
value = 5 + '25' // implicit

Value type checking

console.log(typeof value);

String conversion

Number to string:

value = String(10); /* => '10' */
value = String(10 + 40); /* => '50' */
value = (10 + 40).toString(); /* => '50' */
value = new String(10 + 20); /* => '30' */

Boolean to string

value = String(true); /* => 'true' */
value = String(false); /* => 'false' */

Array to string

value = String([1, 2, 3]); /* => '1,2,3' */
value = String([ ]); /* => '' */ 

Object to string

value = String({name: "Daniel"}); /* => [object Object] */

Conversion to string occurs when any data type is concatenated with a string (implicit conversion):

value = 30 + ' ' + 30; /* => 30 30 */ // Space is considered a symbol.
value = 30 + '' + undefined; /* => 30undefined */

Mathematical operations convert Empty String to zero:

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

In mathematical operations, true is converted to one and false to zero:

value = true + 5; /* => 6 */
value = false + 5; /* => 5 */

String to boolean

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

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

value = Boolean(undefined); /* => false */

Null to boolean

value = Boolean(null); /* => false */

Object to boolean

value = Boolean({}); /* => true */ // An empty object is considered true.

Array to boolean

value = Boolean([]); /* => true */ // An empty array is considered true.

Number type conversion

String to number

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

value = Number(true); /* => 1 */
value = Number(false); /* => 0 */

Null to number

value = Number(null); /* => 0 */

Array to number

value = Number([1, 2, 3]); /* => NaN */ // NaN refers to numbers.

Null type conversion

Converts to zero for mathematical operations:

value = null + 5; /* => 5 */

Undefined type conversion

Converts to NaN for mathematical operations:

value = false + undefined; /* => NaN */

Thank's for reading! ❤️
You can also check out my redux basics cheat sheet


This content originally appeared on DEV Community and was authored by Daniel Krupnyy


Print Share Comment Cite Upload Translate Updates
APA

Daniel Krupnyy | Sciencx (2021-04-07T17:35:52+00:00) JavaScript Basic Type Conversions Cheat Sheet ?. Retrieved from https://www.scien.cx/2021/04/07/javascript-basic-type-conversions-cheat-sheet-%f0%9f%94%a5/

MLA
" » JavaScript Basic Type Conversions Cheat Sheet ?." Daniel Krupnyy | Sciencx - Wednesday April 7, 2021, https://www.scien.cx/2021/04/07/javascript-basic-type-conversions-cheat-sheet-%f0%9f%94%a5/
HARVARD
Daniel Krupnyy | Sciencx Wednesday April 7, 2021 » JavaScript Basic Type Conversions Cheat Sheet ?., viewed ,<https://www.scien.cx/2021/04/07/javascript-basic-type-conversions-cheat-sheet-%f0%9f%94%a5/>
VANCOUVER
Daniel Krupnyy | Sciencx - » JavaScript Basic Type Conversions Cheat Sheet ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/07/javascript-basic-type-conversions-cheat-sheet-%f0%9f%94%a5/
CHICAGO
" » JavaScript Basic Type Conversions Cheat Sheet ?." Daniel Krupnyy | Sciencx - Accessed . https://www.scien.cx/2021/04/07/javascript-basic-type-conversions-cheat-sheet-%f0%9f%94%a5/
IEEE
" » JavaScript Basic Type Conversions Cheat Sheet ?." Daniel Krupnyy | Sciencx [Online]. Available: https://www.scien.cx/2021/04/07/javascript-basic-type-conversions-cheat-sheet-%f0%9f%94%a5/. [Accessed: ]
rf:citation
» JavaScript Basic Type Conversions Cheat Sheet ? | Daniel Krupnyy | Sciencx | https://www.scien.cx/2021/04/07/javascript-basic-type-conversions-cheat-sheet-%f0%9f%94%a5/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.