JavaScript Support automatic type conversion. [Resolved]

Posted by Programmer123 under JavaScript on 9/24/2013 | Points: 10 | Views : 1689 | Status : [Member] | Replies : 6
Can any one have any idea about JavaScript automatic type conversion. Can any one tell me how this will work?




Responses

Posted by: Murugavelmsc on: 9/25/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
http://jibbering.com/faq/notes/type-conversion/
http://www.hscripts.com/tutorials/javascript/type-conversions.php
http://www.typecastjs.org/

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/25/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Sample to understand Automatic Type Conversion in Javascript
function test1(){
var result = (false == 0); //true
alert("test1: " + result);
}
function test2(){
var result = ("10" == 10); //true
alert("test2: " + result);
}
function test3(){
var result = (" " == 0); //true
alert("test3: " + result);
}


1) On test 1 you are comparing a type “Boolean” with an “Integer”, but Javascript will automatically try to convert the Boolean to an Integer to compare them, and if you didn’t know this True is a 1 and False is a 0.
This also happens in most of programming language because Boolean can always be converted to 0 and 1, they actually are 0 and 1.

2) The first value is a string, and the second one is a Integer, they shouldn’t be compared because they have different types, but JS will magically try to convert the string in to a number to compare them, if you try the same example with “hi” == 10 the result will be false.

3) Test 3 proves that JS is made to be simple, comparing an almost empty string with zero will result true. Logically to achieve this in some other languages you would have to trim the string (get rid of the white spaces) and then compare it with an empty string (“”), resulting in a Boolean and then compare that with zero.

References:
http://karlmendes.com/2011/11/understanding-javascript-automatic-type-conversion/
https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-3/type-conversions
http://www.sitepoint.com/automatic-type-conversion/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/25/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can see the below links:-

http://stackoverflow.com/questions/7940506/javascript-automatic-type-conversion-confusion
http://karlmendes.com/2011/11/understanding-javascript-automatic-type-conversion/

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/25/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can see the below links:-

http://www.sitepoint.com/automatic-type-conversion/
http://united-coders.com/matthias-reuter/all-about-types-part-2/
http://blogs.adobe.com/webplatform/2012/08/27/javascript-types/
http://www.javascriptstyle.com/javascript-performs-automatic-data-type-conversion

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/25/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Javascript support automatic type conversion. You should take advantage of it, It is most common way of type conversion used by Javascript developers.

var s = '5';
var a = s*1;
var b = +s;
typeof(s); //"string"
typeof(a); //"number"
typeof(b); //"number"


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 9/25/2013 [Member] Starter | Points: 25

Up
0
Down
hi,
refer this link it might be helpful to you

http://stackoverflow.com/questions/7940506/javascript-automatic-type-conversion-confusion

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response