Type Conversion in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Type conversion in JavaScript is an algorithm that allows us to manipulate the function’s data types from one to another. Also, sometimes, the Javascript compiler automatically does these conversions and ensures that the function gets the valid inputs according to the operator used. In this article, we will dive deep into type conversion in javascript and learn more about these conversions.

Types of type Conversion in JavaScript

  • Implicit Conversion
  • Explicit Conversion

JavaScript Implicit Conversion

Implicit type conversion in Javascript refers as when converting the values of one data type to another automatically.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Implicit Conversion to String

  • Implicit type conversion in javascript to String refers to the automatic conversion of non-string values into string type when they are used in a context where a string is expected.
  • This happens behind the scenes without explicit instruction from the developer.

Example:

Implicit Conversion to Number

  • Implicit type conversion in javascript refers as when converting to Number in JavaScript occurs automatically when non-numeric values are used in numeric contexts, such as arithmetic operations or comparisons.
  • JavaScript tries to convert non-numeric values to numbers to perform the operation.

Example:

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Non-numeric String Results to NaN

In JavaScript, converting non-numeric strings to numbers using Number() results in NaN, indicating "Not-a-Number". For instance, Number("Hello"), Number("true"), or Number("") all yield NaN because these strings lack numeric value. For example:

Implicit Boolean Conversion to Number

Conversion of an expression into a boolean is known as boolean conversion.

null Conversion to Number

When null is implicitly used in a numeric context, such as in arithmetic operations or comparisons, it is converted to the number 0. This implicit conversion ensures that operations involving null behave predictably within numeric contexts.

Example:

In this example, null is implicitly converted to the number 0 when used in addition with the number 10, resulting in the value 10.

undefined used with number, boolean, or null

Utilizing undefined in numeric, boolean, or null operations yields NaN, indicating an undefined or unrepresentable value.

Converting Dates to Numbers

Transforming date objects into numeric values, often represented as milliseconds since January 1, 1970, involves using the getTime() method.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Converting Numbers to Dates

Creating date objects from numeric values entails instantiating a new date using the desired timestamp.

JavaScript Explicit Conversion

The conversion of one data type to another according to our need and using built-in methods is called as explicit type casting in javascript.

We will discuss some built-in methods of javascript type conversion explicitly.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Convert to Number Explicitly

This involves explicitly converting a value to a numeric type using the Number() function.

Convert to String Explicitly

Tis involves convert any other data types to strings, with the help of either String() or toString().

Convert to Boolean Explicitly

This involves explicitly converting a value to a boolean type using the Boolean() function. It ensures that the value is interpreted as either true or false, facilitating boolean operations and logic.

JavaScript Type Conversion Table

In the table below are some of the most common JavaScript values conversion to Number, String, and Boolean :point_down:

ValueString conversionNumber conversionBoolean conversion
20'20'20true
'20''20'20true
false'false'0false
true'true'1true
0'0'0false
'0''0'0true
NaN'NaN'NaNfalse
null'null'0false
undefined'undefined'NaNfalse
[]''0true
""""0false
[23]'23'23true
[10,23]'10,23'NaNtrue
" "' '0true
['fun']'fun'NaNtrue
['fun','enjoy']'fun,enjoy'NaNtrue
function(){}'function(){}'NaNtrue
{}'[object Object]'NaNtrue
Infinity'Infinity'Infinitytrue
-Infinity'-Infinity'-Infinitytrue

Conclusion

  • Type conversion in JavaScript is converting one Data type to another as required for the function to work correctly. The difference between implicit and explicit type conversion is that implicit conversions convert the values automatically, while explicit type conversion is manually done by developers.
  • Explicit conversion has a major role in manipulating data types in the way we want for the specific function.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more