JS Type Conversion 🎯

beginner
14 min

JS Type Conversion 🎯

Welcome to our comprehensive guide on JavaScript Type Conversion! Let's dive into the world of JavaScript and learn how to convert data types like a pro.

Understanding Data Types 📝

Before we delve into type conversion, let's first understand the basic data types in JavaScript:

  1. Number: Integers, Floating-point numbers, and special values like Infinity, NaN, and undefined.
  2. String: Sequence of characters.
  3. Boolean: true or false.
  4. Object: A collection of key-value pairs.
  5. Null: An empty object.
  6. Undefined: A variable that has been declared but has no value.
  7. Symbol: Unique and immutable primitives added in ES6.

Converting Numbers to Strings 💡

Converting a number to a string is often required when we need to concatenate numbers with strings or when working with user input.

Using toString() method

The toString() method converts a number into a string.

javascript
let num = 123; let strNum = num.toString(); // "123"

Using String() constructor

Another way to convert a number into a string is by using the String() constructor.

javascript
let num = 456; let strNum = String(num); // "456"

Converting Strings to Numbers 💡

Converting a string to a number is essential when we perform mathematical operations on strings or when dealing with user input.

Using parseFloat() function

parseFloat() function converts a string into a floating-point number.

javascript
let str = "3.14159"; let num = parseFloat(str); // 3.14159

Using parseInt() function

parseInt() function converts a string into an integer. It can optionally take a second parameter, the base of the number system, which specifies the base of the number in the string.

javascript
let str = "1011"; let num = parseInt(str); // 1011 (base 10) let base2Num = parseInt(str, 2); // 17 (base 2)

Converting Booleans to Numbers 💡

Booleans are converted to numbers in JavaScript, with true being 1 and false being 0.

javascript
let bool = true; let num = Number(bool); // 1 let bool2 = false; let num2 = Number(bool2); // 0

Type Conversion Rules 📝

JavaScript follows certain implicit type conversion rules:

  1. Boolean: Any non-zero number, non-empty string, and any object converts to true. Everything else converts to false.
  2. Number: You can add a string to a number, and the string will be converted to a number.
  3. String: You can concatenate a number and a string.
  4. Object: You can add an object to a number, and the number will be converted to a string and then to a number using the toString() method.
Quick Quiz
Question 1 of 1

Which of the following values will be converted to `false` by JavaScript?

Practical Application 🎯

Let's practice type conversion in a practical scenario:

javascript
let userInput = prompt("Enter a number or a word"); let number = Number(userInput); if (Number.isNaN(number)) { console.log(`Invalid input: ${userInput}`); } else { console.log(`Number: ${number}`); }

In this example, we take user input, convert it to a number, and check if it is a valid number. If it is not a number, we let the user know.

That's it for today! By understanding JavaScript type conversion, you're one step closer to mastering JavaScript. Keep practicing, and you'll soon be able to tackle complex projects with ease. 🚀