Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Type Conversion in Javascript

User image

Published by

sanya sanya

Published at: 2nd May, 2023
2.255 mins read

The process of converting data values from one data type to another is termed Type conversion in Javascript.

For instance, the Type conversion can be understood by a simple example of water. We can freeze water to make ice cubes from it, similarly, we can boil water to make water vapor, and also can melt ice to make water. The process of converting data values from one data type to another is termed Type conversion in Javascript.

However, water in all such cases is like -

Types of Type Conversions

There are three types of Type Conversions used in Javascript which are mentioned below -

String Type Conversion - The conversion of any other data type to a string data type is known as String Conversion.

The Code for the String Type Conversion is mentioned below -

let value = true; alert( typeof value ); // boolean value = String(value); // value is a string "true"
alert( typeof value ); // string

Numeric Type Conversion - The conversion of any other data type to a number data type is known as NumericType Conversion. The Code for the Number Type Conversion is mentioned below -

let str = "123"; alert( typeof str ); // string let num = Number(str); // becomes a number 123
alert(typeof num); // number

Boolean Type Conversion - The conversion of any other data type to a Boolean data type is known as Boolean Type Conversion.

console.log( Boolean(1) ); // true console.log( Boolean(0) ); // false


Implicit and Explicit Type Conversion


The Implicit Type Conversion Takes place when a data type is converted to another data type automatically without telling Explicitly about it.

For instance, the "+" operator does two tasks differently, if uses with integer values, the output will be sum. However, when uses with string, the output will be the concatenation of two strings.

let num = 5; let str = "Hello"; let result = str + num;
console.log(result); // "Hello5"



The Explicit Type Conversion is the Type conversion in which one data type converts to another data type manually.

Some in-built methods support Explicit Type conversion and are mentioned below -

  • parseInt() - converts a string to an integer
  • parseFloat() - converts a string to a floating-point number
  • String() - converts a value to a string
  • Number() - converts a value to a number

Some Examples of Explicit Type Conversion are mentioned below -

let str = "10"; let num = parseInt(str);
console.log(num); // 10

let num2 = 3.14; let str3 = num2.toString();
console.log(str3); // "3.14"

Library

WEB DEVELOPMENT

Basic

HTML - Hyper Text Markup Language

CSS - Cascading Style Sheets

JavaScript

An Introduction to Javascript!

How to Run JavaScript Code

Variables in Javascript

Numbers in JavaScript

JavaScript Operators

Data Types in JavaScript

Conditional Statements

Switch Statements

Loops in Javascript

Arrays in JavaScript

Strings in JavaScript

Objects in JavaScript

Object Methods in JavaScript

Functions in JavaScript

Object Referencing and Copying in JavaScript

' this' keyword

Asynchronous Programming in JavaScript

Callbacks in JavaScript

Promises in JavaScript

Constructor Functions in JavaScript

Async and Await in JavaScript

Type Conversion in Javascript

DOM

Currying in JavaScript

Network Request

Frontend

Backend

Interview Questions

FAANG QUESTIONS