Type Conversions in JavaScript tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to text integers in this tutorial we are going to look at some craziness of chocolate when you try to convert one data type to another since Java was a very flexible language when you try to convert one data type to another in terms of getting error in certain cases it will do the job for you and you might get some unexpected results and a lot of times you can use some of those weirdness of JavaScript to your own advantage and also if you go for a JavaScript interview you might get one of those crazy question even though you know JavaScript very well you may not be able to answer that so going through this tutorial would definitely help alright so let's start with a simple example let's say if you have a variable let X 2 plus 2 where 2 first 2 is a number and a second to the string if I console.log it what do you think this would console oh just think about it don't look for any help just try to get you the answer all right the answer is 22 so why does it give you 22 when you try to add a number and string JavaScript first thinks what is easy to convert if I can work from any number to a string it's very easy one hundred two hundred string easy but if I want to convert hello to a number very hard so javascript always takes an easy path which means if I gave you a number and string it would convert number into a string and then cook kanda so that's why you seeing this result however what if I did this two plus two plus two where the last two is a string what do you think we are going to get here okay so the answer is 42 now why didn't it just convert all the tools to string one of the way it works is it will go from left to right and what it would do is it would take this first row number they are both two numbers so it will add as number and you will get four then it would try to add 4 plus this 2 which is a string and because 2 is a string it will convert 4 to a string because so then you get string 4 and string 2 and it will can can it make it 42 all right I'm going to give you a little bit crazier example 2 plus what do you think I would get here okay so the answer is 3 2 plus 1 is 3 I run it I'll get 3 again the same concept if I want to convert boolean to a number it's very easy true means one false wins 0 and there is only two values it's easy to convert to a number but if I gave you a number five and asked you to convert into boolean very hard so it always takes an easy path and that's why it will if I gave you a boolean and a number it will always convert boolean to a number okay let's make it a little bit complex now what do I get now how a to a number a boolean a straight well the answer is as you guessed or not 32 because the first two you have a number and a boolean so we'll do the same thing when it didn't like the example it's three and then three plus two it would convert three to a string and Concetta you get 32 so first we looked at number and a string now we look at boolean and a number now look at boolean industry so if I did this true plus two I would get as you probably gets it right true too because again it's easier to convert boolean to strings and strings of Orion that's why it's doing it so kind of a true - true string and then can get both strings I'll get true - now what about explicit conversion here it's trying to do it for you but what if you force it to do something that is very hard so for example first of all if I want to convert string to a number I can use this function number and then pass the so let's say if I run this it would come with string to a number two what if I enter hello and try to convert into a number what do you think I would get well the answer is not a number because it's not a number it won't convert it and it would give you this na n not a number what is the type of na n so let's say if I just do type of X what do you think I would get here I run it it's a number so type of na n is a number boolean so if I convert one to boolean it's very easy it would do true one is true zero is false what if I enter five what do you think I would get well the answer is true so the only value that it would give false is zero for everything else it would give you true so all the nonzero values are true so if I put zero here I will get false but ten thousand it's true same thing if I wanna convert string to a boy let's say if I have hello to boolean now here what do you think it would get I run it it would get true if I try to convert string to a boolean if it's an empty string then it would give you false but for all other strings it would give you true so if I put an empty string and run it I would get false so that's the only time you will get false empty string also if I put undefined which is also a type in JavaScript I would get false now it would get false so no undefined empty string and zero gets false everything else gets true even if I have a string called false this would give you true because it's not an empty string all right so I can use this information to my advantage let's say if I have some string and I want to I already know it's a string but it could be undefined as well so I want to check three things if it's not undefined and it's not empty string all I have to do is this F X this would basically examine is some if this is not undefined and not empty string so it will go into this if it's a string with more length more than 0 same thing with the number it will check if it's not undefined no or 0 so instead of doing this if X not equal to let's say on defined for or X not equal to or X X not equal to 0 inter doing all 3 condition you can simply say if X and this should do it to the job so you can really use this type conversion to your advantage but be careful you might get some unexpected result if you are if you miss something now if the value is not a primitive type which means it's an object how it could be an array in that case you don't want to do this if I try something like this let's say try for M cheering you might think that this is this would give you false because it's empty just like empty string this would give you true because objects are always true if I try to convert object to boolean they're always true so don't try to do that instead use something your array length or something to figure out if it's an empty array or not so that's all folks for type conversion in JavaScript I hope you learned something from this tutorial and if you did please like subscribe and provide a constructive comment thank you you
Info
Channel: techsith
Views: 16,605
Rating: undefined out of 5
Keywords: javascript, type conversions, type casting, java script, techsith, techsithtube, tutorials, lessons, techsith.com, isBoolean, isInteger, isNumber, toString, advance javaScript tutorial, javaScript tutues
Id: j9xuvChJftg
Channel Id: undefined
Length: 10min 45sec (645 seconds)
Published: Tue May 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.