Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
TypeScript InstanceType<Type> Utility Type
Next article icon

JavaScript Type Conversion

Last Updated : 01 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In JavaScript Type Conversion can be defined as converting the data type of the variables from one type to the other manually by the programmer(explicitly) or automatically by the JavaScript(implicitly).

  • Implicit Type Conversion (Coercion): Implicit Type Conversion occurs automatically by the JavaScript.
  • Explicit Type Conversion: Explicit Type Conversion occurs when the programmer manually changes the type of the variables using the function Number(), String(), and Boolean().

Implicit Type Conversion (Coercion)

In JavaScript, the implicit type conversion or coercion conversion can be defined as the automatic conversion of the data type of the variables from one type to another type. Implicit type conversion mostly occurs when we are performing the arithmetic or the logical operations.

String + Number (Concatenation)

When we add a string with the number, the JavaScript automatically converts the number into a string and performs string concatenation.

JavaScript
let res = 5 + "5";   console.log(res);   

Output
55 

Boolean to Number

When we perform the mathematical operations, then JavaScript automatically converts true to 1 and false to 0.

JavaScript
let res = true + 1;   console.log(res); 

Output
2 

Equality Comparison (==)

When we use the equality operator in the JavaScript, it compares them after converting the value into the same data type.

JavaScript
let res = (5 == "5");   console.log(res); 

Output
true 

Automatic Conversion in Logical Operations

In JavaScript, these values are automatically converts undefined, null, “” (empty string), false, NaN, and 0 to false, and all other values to true.

JavaScript
let res = Boolean("");   let res2 = Boolean("Hello");   console.log(res) console.log(res2) 

Output
false true 

Explicit Type Conversion

In JavaScript, explicit type conversion can be defined as when we manually change the data type of the variable from one to other using some built-in JavaScript functions. JavaScript provides us the built-in functions for performing the explicit conversion.

Converting to String

In JavaScript, we can convert the value into a string using the String() function, toString(), and by concatenating the empty string.

JavaScript
let n = 123; let s1 = String(n);   let s2 = n.toString();   console.log(s1) console.log(s2) 

Output
123 123 

Converting to Number

In JavaScript, we can convert the value into a number using the Number() function, parseInt(), and parseFloat().

JavaScript
let s = "123"; let n = Number(s);   let s1 = "12.34"; let n1 = parseFloat(s1);   console.log(n) console.log(n1) 

Output
123 12.34 

Converting to Boolean

In JavaScript, we can convert the value into a boolean we can use the Boolean() function.

JavaScript
let str = "Hello"; let b1 = Boolean(str);   let emptyStr = ""; let b2 = Boolean(emptyStr);   console.log(b1) console.log(b2) 

Output
true false 

Why Learn JavaScript Type Conversion?

  • Data Handling: In JavaScript when we are working with API responses, user inputs and calculations, these type of the operation require type conversion.
  • Prevent Bugs: It is important to understand the type conversion which can help in preventing bugs which occurs when JavaScript implicitly converts value.
  • Code Clarity: By performing the explicit conversion makes our code bug free and more clear.

JavaScript Type Conversion Comparison

Conversion Type

Implicit Conversion (Coercion)

Explicit Conversion

String + Number

Automatically converts numbers to strings

Manually convert using String() or .toString()

Number + Boolean

Converts true to 1 and false to 0

Use Number() to explicitly convert

String to Boolean

Non-empty strings convert to true, empty to false

Use Boolean() for clarity

Number from String

Implicit coercion with + operator

Use Number(), parseInt(), or parseFloat()



Next Article
TypeScript InstanceType<Type> Utility Type

V

vivekkothari
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-basics

Similar Reads

  • Type Conversion and Type Coercion in JavaScript
    Data types in JavaScript are flexible due to which the type of the variables can be changed when the program runs. Type Conversion and Type Coercion are the two ways through which we can change the data type of the variables from one type to the other. Type ConversionType Conversion is the process i
    4 min read
  • JavaScript BaseX Converter: Converting Numbers Between Different Bases
    In the world of computer science and mathematics, numbers are represented in various base systems such as decimal, binary, hexadecimal, and octal. Converting numbers between these different base systems is a common requirement. For example, you may need to convert a decimal number to binary, a binar
    3 min read
  • What is a Typed language ?
    Typed Language: Typed languages are the languages in which we define the type of data type and it will be known by machine at the compile-time or at runtime. Typed languages can be classified into two categories: Statically typed languagesDynamically typed languagesStatically typed languages: Static
    4 min read
  • TypeScript InstanceType<Type> Utility Type
    In this article, we are going to learn about InstanceType<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the InstanceType<Type> utility type is used to extract the instance type of a constr
    3 min read
  • Output of Java Programs | Set 21 (Type Conversions)
    Prerequisite - Type Conversions in Java with Examples 1) What is the output of the following program? public class Test { public static void main(String[] args) { int value = 554; String var = (String)value; //line 1 String temp = "123"; int data = (int)temp; //line 2 System.out.println(da
    3 min read
  • Java Program to Implement Type Casting and Type Conversion
    There are many situations that come when we have to change the functionality of the output as well as the type of output according to the need of the requirements. For e.g. Entered PI value in decimal format should be converted in the decimal format so that value can be used efficiently without any
    3 min read
  • Typecasting in Java
    Typecasting in Java is the process of converting one data type to another data type using the casting operator. When you assign a value from one primitive data type to another type, this is known as type casting. To enable the use of a variable in a specific manner, this method requires explicitly i
    4 min read
  • Field getGenericType() method in Java with Examples
    The getGenericType() method of java.lang.reflect.Field used to return a Type object representing the declared type of this Field object. The returned type object can be one of the implementations of Type's subinterfaces: GenericArrayType, ParameterizedType, WildcardType, TypeVariable, Class. If the
    2 min read
  • Java Program to Convert Int to Double
    Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types - boolean, byte, char, short, int, long, float, and double. These data types
    4 min read
  • Java Program to Convert int to long
    Given a number of int datatype in Java, your task is to write a Java program to convert this given int datatype into a long datatype. Example: Input: intnum = 5 Output: longnum = 5 Input: intnum = 56 Output: longnum = 56 Int is a smaller data type than long. Int is a 32-bit integer while long is a 6
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences