What is Type Coercion 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 Coercion in JavaScript

Type Coercion in JavaScript refers to the process of conversion of values from one data type to another automatically. There are many data types like string, number, boolean, etc, So it is essential to know how these data types are coerced from one data type to another. In this article, we will learn, "What is Coercion in JavaScript?"

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

What is Type Coercion in JavaScript?

Type coercion in JavaScript refers to the automatic conversion of values from one data type to another.

  1. Numeric to String Coercion: Numeric values are automatically coerced to strings when concatenated with strings. This can be useful for formatting output or generating dynamic strings. Example:

  2. String to Numeric Coercion: Strings containing numeric characters are automatically coerced to numbers when used in mathematical operations. This allows for arithmetic operations involving string representations of numbers.

    Example:

  3. Boolean to Number: In numerical contexts, boolean values (true and false) are automatically coerced to numbers (1 and 0 respectively). This allows for boolean values to be used in arithmetic operations.

    Example:

  4. Comparison Coercion: In JavaScript, comparison operators (== and ===) perform type coercion. The == operator coerces values to the same type before comparison, while === strictly compares values without coercion.

    Example:

Rule of Type Coercion

The rules governing type coercion in JavaScript include:

  1. Any numbers, boolean values are converted to strings in where strings are expected, such as concatenation with strings or when using string functions.
  2. Non-numeric values are converted to numbers in where numbers are required, such as arithmetic operations or comparisons.
  3. Values are converted to booleans where booleans are expected, such as logical operations or conditional statements.
  4. If a conversion is not possible, it results in NaN (Not-a-Number), typically in arithmetic operations involving non-numeric values.

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

Type Coercion for Objects

  • In JavaScript, objects can undergo type coercion when they are used in contexts that expect primitive values like strings or numbers.
  • This coercion typically involves invoking specific methods or performing conversions based on the object's internal representation.

Example

In the above example, we can see that the addition of the variable num and object myContainer is returned as 20, which is correct. It is because of the valueOf() method in the object 'myContainer' which returned 8. In case the value of the method is not present in the object, NaN is returned.

Example of Type Coercion

In this example, the variable num is assigned the string "5". When num is used in a mathematical operation (*), JavaScript implicitly coerces it to a numeric value, resulting in the numeric value 10 as the output. This demonstrates how JavaScript automatically converts a string containing numeric characters to a number when used in a numerical context.

Conclusion

Conclusion:

  1. Type coercion in JavaScript automatically converts values from one data type to another.
  2. It includes conversion from Number to String, Boolean to Number, String to Number, etc.
  3. In Javascript, we can also convert objects into primitive values.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more