DEV Community

Cover image for JavaScript: Type Conversion
4 1

JavaScript: Type Conversion

To Binary

JavaScript has two boolean values: true and false. But, it also treats certain values as truthy or falsy. All values are truthy except 0, null, undefined, "", false, and NaN.

We can switch values between true and false using the negation operator !. This conversion also converts the type to boolean.

const a = null;
const b = undefined;
const c = "";
const d = 0;

console.log(typeof a); // object
console.log(typeof b); // undefined
console.log(typeof c); // string
console.log(typeof d); // number

const w = !a;
const x = !b;
const y = !c;
const z = !d;

console.log(typeof w); // boolean
console.log(typeof x); // boolean
console.log(typeof y); // boolean
console.log(typeof z); // boolean

This did change the type to boolean, but it also switched the value. If you need conversion to boolean but stay on the same truthy or falsy side, use !! 🀯

const a = null;
const b = undefined;
const c = "";
const d = 0;

console.log(typeof a); // object
console.log(typeof b); // undefined
console.log(typeof c); // string
console.log(typeof d); // number

const w = !!a;
const x = !!b;
const y = !!c;
const z = !!d;

console.log(typeof w); // boolean
console.log(typeof x); // boolean
console.log(typeof y); // boolean
console.log(typeof z); // boolean

// Let's check if they are all false though and haven't switched to true!

console.log(w); // false
console.log(x); // false
console.log(y); // false
console.log(z); // false

To String

Use toString() method.

const num = 7;
console.log(typeof num); // number
const numString = num.toString();
console.log(typeof numString); // string

Or use the shortcut by appending to "" 🀯

const num = 7;
console.log(typeof num); // number
const numString = num + "";
console.log(typeof numString); // string

To Number

parseInt() function parses a string and returns an integer. You pass the string as the first parameter and the second parameter is radix. It specifies which numeral system to use: hexadecimal (16), octal (8), or decimal (10).

console.log(parseInt("0xF", 16)); // 15
console.log(parseInt("321", 10)); // 321

Or use the shortcut by adding + operator in front of the string! 🀯

console.log(+"0xF"); // 15
console.log(+"321"); // 321

There are situations where the + operator might be used for concatenation. In that case, use the bitwise NOT operator ~ twice.

console.log(~~"0xF"); // 15
console.log(~~"321"); // 321

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (2)

Collapse
 
siddsarkar profile image
Siddhartha Sarkar β€’

Great job writing these.!

Collapse
 
bhagatparwinder profile image
Parwinder πŸ‘¨πŸ»β€πŸ’» β€’

Thanks a ton! I appreciate it β™₯️

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay