loading...
Cover image for JavaScript: Type Conversion

JavaScript: Type Conversion

bhagatparwinder profile image Parwinder 👨🏻‍💻 ・2 min read

JavaScript with Parwinder (39 Part Series)

1) What is Javascript? 2) Where can I run JavaScript? 3 ... 37 3) Types in JavaScript 4) What are objects in JavaScript? 5) Reference vs Value 6) Arrays in JavaScript 7) Array Methods in JS 8) Array Instance Methods 9) Array Instance Methods II 10) Functions in JavaScript 11) Type of Functions in JavaScript 12) Scope in JavaScript 13) Hoisting in JavaScript 14) Closures in JavaScript 15) Debugging in JavaScript: Console Methods 16) The new Keyword in JavaScript 17) JavaScript: Type Conversion 18) JavaScript: While Loop 19) JavaScript: Do While Loop 20) JavaScript: For Loop 21) Lesser-Known JavaScript Tricks 22) Events & Event Handling 23) Event Bubbling 24) Event Capturing 25) JS: Switch Statement Explained 26) Arrow Function: Basics 27) this Keyword in JavaScript 28) Arrow functions & this keyword 29) Callback Functions & Callback Hell 30) Strict Mode in JavaScript 31) Optional Chaining 32) Timers & Intervals 33) Arrow Function and The new, arguments & super Keyword! 34) Promises: Introduction 35) Promises: Chaining, Error Handling & Operators 36) New Promise Methods: allSettled & any 37) Async/Await: Introduction 38) Async/Await: Common Mistakes 39) Async/Await: Error Handling

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

JavaScript with Parwinder (39 Part Series)

1) What is Javascript? 2) Where can I run JavaScript? 3 ... 37 3) Types in JavaScript 4) What are objects in JavaScript? 5) Reference vs Value 6) Arrays in JavaScript 7) Array Methods in JS 8) Array Instance Methods 9) Array Instance Methods II 10) Functions in JavaScript 11) Type of Functions in JavaScript 12) Scope in JavaScript 13) Hoisting in JavaScript 14) Closures in JavaScript 15) Debugging in JavaScript: Console Methods 16) The new Keyword in JavaScript 17) JavaScript: Type Conversion 18) JavaScript: While Loop 19) JavaScript: Do While Loop 20) JavaScript: For Loop 21) Lesser-Known JavaScript Tricks 22) Events & Event Handling 23) Event Bubbling 24) Event Capturing 25) JS: Switch Statement Explained 26) Arrow Function: Basics 27) this Keyword in JavaScript 28) Arrow functions & this keyword 29) Callback Functions & Callback Hell 30) Strict Mode in JavaScript 31) Optional Chaining 32) Timers & Intervals 33) Arrow Function and The new, arguments & super Keyword! 34) Promises: Introduction 35) Promises: Chaining, Error Handling & Operators 36) New Promise Methods: allSettled & any 37) Async/Await: Introduction 38) Async/Await: Common Mistakes 39) Async/Await: Error Handling

Posted on by:

bhagatparwinder profile

Parwinder 👨🏻‍💻

@bhagatparwinder

Happy, cheerful, confident. Web developer based out of Chicago :)

Discussion

markdown guide