+ 4
What is meant by implicit coercion in Javascript ?
It has got to do something with variables and data type i dunno exactly please help me understand with example if anyone knows...
7 Answers
+ 1
as far as i know, PHP has some sort of implicit coercion that is so cool, but works different from javascript .. let me show you how by using two simple examples:
if you add a string to a number using the plus sign (+) then php will automatically assumes that you are trying to make a calculation and hence it coerces/converts the string into a number like so: 4 + "4" = 8
however, if you add a string to a number using the dot operator (.) then it will coerce the number into a string as it will assume that you are trying to concatenate two strings together .. like so:
4 . "4" = 44
+ 4
Thanks bro is this true only for Javascript ?
+ 4
thanks man great answer..
+ 2
implicit: means happening behind the scenes by the javascript engine that handles your code.
coercion: in Javascript means converting a type into another type.
implicit coercion: simply means that javascript engine converts a data type without asking your permission into another type depending on how much relevant it is.
example:
var a = 4;
var b = "4";
console.log(a + b);
This will output 44 instead of 8 .. because javascript coerced/converted the a variable from number into a string
+ 1
you can try it...
https://code.sololearn.com/w8xay404ihgO/?ref=app
+ 1
and this simple one for javascript:
https://code.sololearn.com/W04i6LXsUXv4/?ref=app
0
You're welcome any time bro.