JavaScript Built in Functions | Using Predefined Functions In JavaScript

JavaScript is a programing language that provides you with a lot of JavaScript built in functions and features for you to use. We will try to cover them as quickly as possible. They have their own benefits for the user. Lets discuss them with the help of some examples.

Always try to use the JavaScript built in features like JavaScript built in functions and JavaScript built in libraries. There are a lot of JavaScript built in functions and built in libraries in JavaScript. For example date, Math, string, regular expression classes. Also like the array methods push(), pop(), sort() and reverse(). We try to use these because these are the codes which the JavaScript interpreter or the browser has given us to reduce work. It means that you don’t have to write like a sort() function by yourself it is there to be used. As the browsers or the interpreters are written in the compiled languages like C++ or Java etc. they are more efficient than the JavaScript code.

Let’s see the example below:

function bCheckAlphaNumeric(s){//checking for alphanumeric characters in s

var reAlphaNumaric = /^[a-zA-Z0-9]+$/;

return reAlphaNumaric.test(s);

}

In the above code if you have to write the comparison by yourself then you have to do it with a loop to counter and compare it with each character in the string. So we have reduced a very long function into just two lines of code.

Anonymous functions are another JavaScript built in feature of JavaScript language. They can be used or passed to a function as arguments. They can have no name and declared inline. They can be used like other variables in JavaScript. It means they can be just declared and assigned like other variables. Look at the example below to understand more.

Example:

window.onload = function() {

alert(“Welcome to howtocode.pk”);

}

As you can see in the above example we are using an anonymous function. Here we are using and declaring the function at the same time in the above example.

Other example is like when you use the sort() function on an array as Array.sort(). This can be used in two ways like with parameters and without parameters. When you don’t give any parameters to the sort() function, the array will be sorted in the alphabetically and ascending (might be first converted into strings if it is possible). You can call the sort() function with a function parameter for more advanced sorting we are not going deep in this now.

The continue statement can be skipped with the use of properties of an object dynamically in a loop. Let’s see the example to show you more.

Example:

For (i=0; i<myVar; i++){

If(!condition){

Continue;

}

.

.

.

}

In the above the example we are using a continue statement if the condition is true then skip the preceding code and continue with the next iteration in the loop. The same can be achieved with JavaScript built in features with the properties of an object dynamically. Lets see the example to understand more.

Example:

myVar = 0.0

for(stock in portfolio){

myVar += portfolio[stock];

}

In this example we are getting the exact output as from the previous one but in this example we are using the built in feature of JavaScript. We will discuss it more a bit later.

Comments