The JS eval Function

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to demonstrate how to use the eval function so here I've got a program that will convert ounces to pounds so they have to do is just enter ounces I've got a Clear button that clears it clears both the message and what's in the text box so that we can do it again what we want to do is expand this so that somebody could add some several ounces together and then and then convert the sum of them so notice that when I do that it just uses whatever's there it's not able to do the math and reports not a number as the pounds so one way that I can fix this is to use the eval function and the JavaScript eval function takes a string which we always get a string from the textbox write whatever the input whatever is input in the textbox is a string and that's what we're doing we're getting that value in that text box so the file function takes a string and tries to convert it to math or try to do the math convert it to a mathematical expression and then it give produces the results of that mathematical expression so if I go ahead and get that value from that textbox and then I do the eval function on it what is going to do is try to evaluate it mathematically and produce a result so let's see how that works so now we do 23 plus 54 plus 16 and convert and it added that all up to 93 and and then it did the conversion so it worked out great for us now there's a couple of things to worry about when you do the eval function so one of the the eval function does one of two things it either takes a string and tries to convert it to mathematical expression or it tries to execute it so if I enter a command a JavaScript command it actually will execute it so consider if I enter this statement right notice it'll just be read it as a string but notice that this is a JavaScript command now when they push convert the Val function actually executed and my web page is gone now this is pretty benign all it is is document dot write which just replaces whatever you have on your page with whatever the write told it to do so we won't do much damage but you can imagine there are Java scripts commands that could be executed and cause a lot of problems so when you use the eval function it's really important that you don't allow any input from a user to get fed to the eval function and one way to stop that is actually in this textbox I'm going to set it to read-only so here I'm setting a parameter the attribute read-only and you don't have a get rid of value you can say read-only equals read only but html5 no longer requires it so the style the preferred way to do it is to just put the attribute itself and this will make it read-only now if I try to put something in this textbox so I try to copy paste in it it won't work if I try to type in it so I'm clicking on it but it's not even showing up active so now the only way that users can enter it is through these keys that I've programmed in to do certain things so they can't make up things to put in there it just has to respond to these keys that are in there so now it can do those conversions so that's how you can use the eval function notice it can do multiplication as well to evaluate something that is that a string that is sent to it that is more complex than just entering it
Info
Channel: Linda DuHadway
Views: 2,963
Rating: 4.8356166 out of 5
Keywords: eval, JavaScript, JS eval function
Id: uyEhYbjIQzw
Channel Id: undefined
Length: 4min 3sec (243 seconds)
Published: Mon Apr 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.