JavaScript Lessons

JavaScript Type Conversion: Strings, Numbers, and Booleans

JavaScript type conversion lets you turn values into strings, numbers, booleans, objects, or arrays so your code works with the data it expects.

Serhat ALPASLAN3 min read

Contents

In JavaScript, type conversions are the process of converting one data type into another. This operation is very common in programming languages. JavaScript also supports many different type conversions. In this article, we’ll cover type conversions in JavaScript in detail and explain them with examples.

1. String Conversion

In JavaScript, the toString() function is used to convert a value to the string data type. This function returns the string representation of an object. For example:

let sayi = 42;
let sayiString = sayi.toString();
console.log(typeof sayiString); // "string"

In this example, the sayi variable is first defined as a number. Then, the sayi variable is converted to the string data type using the toString() function. Finally, the data type of the sayiString variable is checked using the typeof operator, and the result “string” is returned.

2. Number Conversion

In JavaScript, the Number() function is used to convert a value to the number data type. This function returns the numeric representation of an object. For example:

let sayiString = "42";
let sayi = Number(sayiString);
console.log(typeof sayi); // "number"

In this example, the sayiString variable is first defined as a string. Then, the sayiString variable is converted to the number data type using the Number() function. Finally, the data type of the sayi variable is checked using the typeof operator, and the result “number” is returned.

3. Boolean Conversion

In JavaScript, the Boolean() function is used to convert a value to the boolean data type. This function returns the boolean representation of an object. For example:

let sayi = 42;
let booleanDeger = Boolean(sayi);
console.log(booleanDeger); // true

In this example, the sayi variable is first defined as a number. Then, the sayi variable is converted to the boolean data type using the Boolean() function. Finally, the booleanDeger variable is printed with the console.log() function, and the result “true” is returned.

4. Object Conversion

In JavaScript, the Object() function is used to convert a value to the object data type. This function returns the object representation of an object. For example:

let sayi = 42;
let obje = Object(sayi);
console.log(typeof obje); // "object"

In this example, the sayi variable is first defined as a number. Then, the sayi variable is converted to the object data type using the Object() function. Finally, the data type of the obje variable is checked using the typeof operator, and the result “object” is returned.

5. Array Conversion

In JavaScript, the Array() function is used to convert a value to the array data type. This function returns the array representation of an object. For example:

let sayilar = Array(1, 2, 3, 4, 5);
console.log(sayilar); // [1, 2, 3, 4, 5]

In this example, the sayilar variable is converted to the array data type using the Array() function. Finally, the sayilar variable is printed with the console.log() function, and the result “[1, 2, 3, 4, 5]” is returned.

In JavaScript, type conversions are very common. In this article, we covered the most commonly used type conversions in JavaScript in detail and explained them with examples. With these examples, you can better understand how type conversions are done and what they are used for.

Share it if it helped

Write a comment