AceDevHub
Beginner JavaScript Interview QuestionsBeginnerConcept

JavaScript · Question 13

What is type coercion in JavaScript? Explain implicit and explicit coercion.

Direct answer

Type coercion is conversion from one type to another; it is implicit when JavaScript performs it automatically and explicit when your code requests the conversion.

An explicit conversion is visible in code, for example Number('42'), String(42), or Boolean(value). An implicit conversion happens as part of an operation.

For example, 1 + '2' produces the string '12' because the string operand causes concatenation, while '6' - 1 produces the number 5 because subtraction requires numeric operands.