JavaScript Built-in Functions

            Whenever developing a project, you will have common solution that will come up and it is always good practice to keep track of these solutions to speed up development in the future. That is why many modern programming languages have a vast amount of built in function for you to use. Predefined functions are great for many reasons, top of which is it just makes everything simpler. Having to write your own function for every little sub problem is terribly inefficient and time consuming its always best to use a predefined function when you can. We will go over some of the most useful or common ones and explain each of them.

eval()

            Evaluate is a simple enough function like most built-in functions are. Eval() simply evaluates a piece of JavaScript code represented as a string. So, if you want if you want to evaluate two plus two you can do so.

eval(‘2 + 2’)    

  • 4

eval(‘2 + 2’) === eval(‘4’)

  • true

eval(“alert(‘this is executed by eval()’)”);

I think you understand how eval() works you can run any snippet of code you want in any part of your code.

While I am sure you can think of many uses for eval() and there are many cases where its useful and safe to use. Eval() is  considered a risky built in function as it has two major problems performance and security. Performance wise eval() runs its own interpreter/compiler this can be a big hit on performance as your code will need run a potentially heavy compiler in the middle of run time if your code is compiled. With this is most likely not detrimental to your code as JavaScript is mostly an interpretive language. The second risk on security is code  injections (1), if you are not familiar this is where a user types a particular kind piece of code into input portal of your application and your code runs that code by accident because you didn’t check the input. This is a huge security risk this is the main reason to be sparing of where you use eval(). In fact, I would suggest using eval independent of any user input as injections are some the most common security flaws in software and eval() is a very easy gateway.

parseInt()

            ParseInt is an extremely simple function, all it does is check if a string is an int and returns the integer it parsed from the string.

var b = parseInt(“10.00”)

b = 10

ParseInt does not really have any downside and obvious use is for when you are converting types. There are many more conversion built in function that might be useful to look up depending on your needs.

Escape and Unescape

            Lastly lets cover escape() and unescape() they are both remarkably similar so I decided to group them. They both convert text into a value, the difference being escape() return the hexadecimal encoding of the argument where as unescape() returns the ASCII value. Both have uses in many areas when you want to know data size, comparing values and so on.

            I hope this brief overview of some of these functions was useful happy coding!

Sources:

  1. https://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil
  2. https://www.tutorialspoint.com/javascript/javascript_builtin_functions.htm

Leave a comment

Design a site like this with WordPress.com
Get started