Coercion in javascript

Something Like that:
Coercion

What is Coercion
converting a value from one type to another (this happens quite often in Javascript because it’s dynamically typed)

For that you need to create first html code.

HTML

<p>Value of a =  <span id="demo"></span></p>

After that add this JS code.

JS

var a = 1 + '2';
document.getElementById("demo").innerHTML = a;

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”qXaxev” default_tab=”result” user=”pradeepanvi”]See the Pen Coercion in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Now you can see

Value of a = 12

Why it is?
Because String and number will not equal. For that you need to convert string into number as well. For this click here.

Leave a Reply

Your email address will not be published. Required fields are marked *