
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How does Implicit coercion differ from Explicit coercion in JavaScript?
In this article, you will understand how implicit coercion differs from Explicit coercion in JavaScript.
An implicit coercion is an automatic conversion of values from one datatype to another. It is also known as type conversion.
An explicit coercion is the conversion of data type depending on the user's necessity.
Example 1
In this example, let us learn about implicit coercion.
let inputValue = "5" console.log("The input variable is defined as: ") console.log(inputValue, typeof inputValue); let resultValue = Number(inputValue); console.log("
The input variable is defined as: ") console.log(resultValue, typeof resultValue);
Explanation
Step 1 −Define a variable: inputValue and assign an integer.
Step 2 −Add an empty string to ‘inputValue’. Now the type of ‘inputValue’ is changed from number to string.
Step 3 −Display the value and its type as result.
Example 2
In this example, let us learn about explicit coercion.
let inputValue = "5" console.log("The input value is defined as a string with value: ", inputValue) let resultValue = Number(inputValue); console.log("The result value after conversion to a number is :", resultValue)
Explanation
Step 1 −Define a variable: inputValue and assign a string value to it.
Step 2 −Typecast the string value to integer. Now the type of ‘inputValue’ is changed from string to number.
Step 3 −Display the value and its type as result.
- Related Articles
- JavaScript : Why does % operator work on strings? - (Type Coercion)
- What is JavaScript type coercion?
- Argument Coercion in C/C++?
- Coercion: Definition and Meaning
- Explain non-boolean value coercion to a boolean one in JavaScript?
- Determine common type following standard coercion rules in Python
- Types of Polymorphisms - Ad-hoc, Inclusion, Parametric & Coercion
- What are implicit and explicit type conversions in C language?
- How does Capital Gearing differ from Income Gearing?
- How does a Planet differ from a Star?
- How does a proton differ from an electron?
- How does an electron differ from a neutron?
- What are the terms consent, free consent, coercion, undue influence, fraud and misrepresentation?
- What is the difference between implicit and explicit type conversion in C#?
- What are the differences between implicit and explicit waits in Selenium with python?
